Parker Song
Parker Song

Reputation: 151

How to get the param from another controller when using the 'ui-router'?

This is the view code:

<a type="button" ui-sref="index.usermng.userInfo" ng-click="checkUserInfo(item.id)" class="btn btn-primary">check</a>

Controller:

//UserManage Controller
userApp.controller('userCtrl', ['$scope', '$http', '$location', 'serverUrl', function($scope, $http, 
$scope.checkUserInfo = function(userId) {
    console.log(userId);//I can get userId in here
    $scope.$broadcast('toUserInfo',userId);
}
}]);

//UsrInfo Controller
userApp.controller('userInfoCtrl', ['$scope', '$http', 'serverUrl', function($scope, $http, serverUrl) {

$scope.$on('toUserInfo',function(data){
    console.log("in.....");
    console.log(data);
})

How to get the 'userId' from 'userCtrl' in 'userInfoCtrl'?

Upvotes: 0

Views: 38

Answers (1)

Huy Hoang Pham
Huy Hoang Pham

Reputation: 4147

Using rootScope to communicate between controllers

userApp.controller('userCtrl', ['$scope', '$http', '$rootScope', 
function($scope, $http,$rootScope) { 
    $scope.checkUserInfo = function(userId) {
       console.log(userId);//I can get userId in here
       $rootScope.$broadcast('toUserInfo',userId);
    }
}]);

Now the userInfoCtrl can get the userId from userCtrl.

Upvotes: 1

Related Questions