How can I run this function with $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>
Here are my controllers:
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();
});
Alert shows up when page refresh but i want to show it after click when ng-click
If you want to assign the test
function to $scope.value
, just remove the ()
in the following line:
$scope.value = $rootScope.test;
See the fiddle .
You need to set $scope.value to $rootScope.test
Setting it to $rootScope.test() invokes the function and does not actually set value to a reference to the function in $rootScope.
$scope.value = $rootScope.test
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.