简体   繁体   English

AngularJS控制器范围

[英]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. 我不理解,因为当go变量更改progressRun函数中的值时, console.log不会显示正确的值,并且它在progressRun函数之前和之后调用。

Simply move your console.log() after you change the go variable in the progressRun() function...: 更改progressRun()函数中的go变量后,只需移动console.log()

 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: 更新:如果你要记录的变量值随时随之改变(即使它的值应修改progressRun()函数,使用:

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

Just after go variable initialization (as correctly @Shreevardhan suggestes). 刚过go初始化变量(如正确@Shreevardhan suggestes)。

Either log the value after changing 更改后记录值

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

Or, use $watch on the variable 或者,在变量上使用$watch

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

PS: $watch will log each time the value changes. PS: $watch将在每次值更改时记录日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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