使用'ui-router'时如何从另一个控制器获取参数?

时间:2015-07-30 14:51:03

标签: angularjs

这是视图代码:

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

控制器:

//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);
})

如何从'userInfoCtrl'中的'userCtrl'获取'userId'?

1 个答案:

答案 0 :(得分:1)

使用rootScope在控制器之间进行通信

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);
    }
}]);

现在userInfoCtrl可以从userId获得userCtrl