在.run下的app.js中
$rootScope.uPLevel = 0;
.controller
$scope.$watch($rootScope.uPLevel,function() { $scope.userPLevel = $rootScope.uPLevel; },true);
在.run下的app.js中
$rootScope.uPLevel = 0;
.controller
$scope.$watch($rootScope.uPLevel,function() { $scope.userPLevel = $rootScope.uPLevel; },true);
$scope.$watch(function() { return $rootScope.uPLevel; },function() { $scope.userPLevel = $rootScope.uPLevel; },true);
两个旁注:
>将这个值存储在服务中而不是$ rootScope可能是谨慎的。>如果uPLevel只是一个整数(如你的例子所示),那么你不需要传递true作为第三个参数 – 这只适用于数组和对象。如果您确实想要收看,那么我建议使用$ watchCollection。