admin管理员组文章数量:1427586
I get the value of my $rootScope inside my function But I wanna get thsi value inside all the functions of my Controller to passe it to another controller: Can You Help me please
MY HTML:
<input type="checkbox" ng-model="u.selected" data-ng-click="consoleClient(u)">
my Script Code :
$scope.client = {
id : null,
nom : '',
nberPhone : '',
adresse : '',
selected : false
};
$scope.consoleClient = function(client) {
$rootScope.test = client;
console.log(" lll "+$rootScope.test);
};
console.log(" aaaa "+$rootScope.test);
The console log is returning the correct result But the second Out of the function Is returning undefined. Can you explain the reason for me please.
I get the value of my $rootScope inside my function But I wanna get thsi value inside all the functions of my Controller to passe it to another controller: Can You Help me please
MY HTML:
<input type="checkbox" ng-model="u.selected" data-ng-click="consoleClient(u)">
my Script Code :
$scope.client = {
id : null,
nom : '',
nberPhone : '',
adresse : '',
selected : false
};
$scope.consoleClient = function(client) {
$rootScope.test = client;
console.log(" lll "+$rootScope.test);
};
console.log(" aaaa "+$rootScope.test);
The console log is returning the correct result But the second Out of the function Is returning undefined. Can you explain the reason for me please.
Share Improve this question edited Apr 2, 2016 at 17:16 Alon Eitan 12k8 gold badges51 silver badges60 bronze badges asked Apr 2, 2016 at 17:08 yeddezyeddez 3651 gold badge4 silver badges14 bronze badges1 Answer
Reset to default 7Yes, because you haven't call to $scope.consoleClient
so it's not defined outside the function scope. It will work after you call the function:
$scope.consoleClient = function(client) {
$rootScope.test = client;
console.log(" lll "+$rootScope.test);
};
$scope.consoleClient('clientName');
console.log(" aaaa "+$rootScope.test); // output: ' aaaa clientName'
If you want to watch for changes of the variable outside the function, you can do the following:
$scope.$watch(function() {
return $rootScope.test;
}, function(newValue, oldValue) {
console.log('Old value: ', oldValue);
console.log('New value: ', newValue);
}, true); // Note the "true" - Compare the object using "angular.equal"
本文标签: javascriptrootScope is not working with me in AngularJsStack Overflow
版权声明:本文标题:javascript - $rootScope is not working with me in AngularJs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745501474a2661051.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论