如何使用$ rootScope运行此功能?
Fiddle
<div ng-app="myApp" ng-controller="parentCtrl">
{{test}}
<div ng-controller="myCtrl1">
<div ng-controller="myCtrl2">
<a href="#" ng-click="value()">show me</a>
</div>
</div>
</div>
这是我的控制器:
angular.module('myApp', []).controller('parentCtrl', function($scope, $rootScope) {
$rootScope.test = function() {
alert("asdasd");
}
});
angular.module('myApp').controller('myCtrl2', function($scope, $rootScope) {
$scope.value = $rootScope.test();
});
警报会在页面刷新时显示,但我想在ng-click
答案 0 :(得分:0)
答案 1 :(得分:0)
您需要将$ scope.value设置为$ rootScope.test
将其设置为$ rootScope.test()会调用该函数,并且实际上并未将值设置为$ rootScope中对该函数的引用。
$scope.value = $rootScope.test