最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $rootScope is not working with me in AngularJs - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 7

Yes, 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"
发布评论

评论列表(0)

  1. 暂无评论