简体   繁体   中英

AngularJS controller scope

I have a controller:

appControllers.controller('MeetingListController', ['$scope', '$route', '$location', function($scope, $route, $location){
    console.log($scope.go);

    $scope.progressRun = function($event){
        $scope.go = true;
    };
});

I don't undersand because when go variable changes value in progressRun function, console.log doesn't show right value and it called before and after progressRun function.

Simply move your console.log() after you change the go variable in the progressRun() function...:

 appControllers.controller('MeetingListController', ['$scope', '$route', '$location', function($scope, $route, $location) {
    $scope.go = 'initial value you want to assign to this variable';

    $scope.progressRun = function($event) {
        $scope.go = true;
        console.log($scope.go);
    };

 }]);

UPDATE: If you want to log the variable value anytime it changes (even when it's value should be changed outside progressRun() function, use:

$scope.$watch('go', function() {
    console.log($scope.go);
});

Just after go variable initialization (as correctly @Shreevardhan suggestes).

Either log the value after changing

$scope.go = true;
console.log($scope.go);

Or, use $watch on the variable

$scope.$watch('go', function() {
    console.log($scope.go);
});

PS: $watch will log each time the value changes.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM