0

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'?

7
  • What is data in the $on? Commented Jul 30, 2015 at 14:52
  • @tymeJV nothing in console Commented Jul 30, 2015 at 14:54
  • Show the controller relationships. Are you sure it is parent/child ? Are you sure item.id is defined when you broadcast? Commented Jul 30, 2015 at 15:01
  • @charlietfl updated my code,is it parent/child? Commented Jul 30, 2015 at 16:04
  • Not enough information is shown to know what the relationship is. Are you using a router? Or just declaring them the controllers in the html? Commented Jul 30, 2015 at 16:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks,but I find this issue in another question:[stackoverflow.com/questions/11252780/… first answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.