0

I am using two controller in same page.

First Controller has the Two tabs. First Tab has contact list and second tab has Favourites List.

In First Tab. I am showing all the datas. In second Tab, I have showing datas based on isFavourites field

ng-show="user.isFavourites"

If it is true. it is showing.

When i clicking the contact list on First Tab. Popup Will show. In that popup, I have one button that is AddFav. Popup is a second Controller.

When i clicking on AddFav button I have setting isFavourites is true.

So my question is

After setting isFavourites, When i goto the second tab, The data will show with last updated Filed.

My Form is

<div id="userListControl">
  <div id="tab1">...</div>
  <div id="tab2">...</div>
</div>
<div id="userDetailsControl">

</div>


userDirectory.controller("userListControl", function($scope,$rootScope, $http)
        {
            $http.get('data/userData.json').success (function(data){
               $scope.users = data;
            });

            $scope.selectUser = function(user){
                $rootScope.selectedUser = user;
                showPopup();
            }
        });
    });

    // Contact Details Controller with Watch()
    userDirectory.controller("userDetailsControl", function($scope, $rootScope){
        $rootScope.$watch("selectedUser", function(newVal){
            $scope.user = newVal;
        });

        $scope.addFav = function(user){
            $rootScope.selectedUser.isFavourites = true;
            console.log($rootScope.selectedUser)
        }
    });

In first control, Selectuser is used for when clicking on contact list, It will shows that data in popup

Mohaideen Ismail
  • 314
  • 4
  • 21
  • possible duplicate of [Passing data between controllers in Angular JS?](http://stackoverflow.com/questions/20181323/passing-data-between-controllers-in-angular-js) – Johny T Koshy Nov 04 '14 at 06:18
  • Somehow it is different, I have passing the control between two controller. Already i have used rootScope in watch directory. Again i have to use rootScope in Watch – Mohaideen Ismail Nov 04 '14 at 06:35
  • you should try event broadcasting or services for controller communication. – Sten Muchow Nov 04 '14 at 06:44

1 Answers1

0

I got the solution for this.

I am using broadcasting for this.

In Second Controller

$scope.addFav = function(user){
            $scope.user.isFavourites = true;
            $rootScope.$broadcast('addFavourites', $scope.user);
        }

In First Controller

$scope.$on('addFavourites', function (event, data) {
// Here I am doing my stuff..
});
Mohaideen Ismail
  • 314
  • 4
  • 21