angular.js - Ask a little question about angularjs
天蓬老师
天蓬老师 2017-05-15 17:09:02
0
5
519

I am a newbie in angularjs. I encountered such a problem in the project today
I want to hide p after 1S, but it has no effect.
Please tell me why there is no response

<p ng-show="on">111</p>

app.controller('myinfoCtrl', ['$scope'],function($scope) {
    $scope.on=true;
    setTimeout(function(){
        console.log($scope.on)
        $scope.on=false;
        console.log($scope.on)
    },1000)
}
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(5)
世界只因有你
<!DOCTYPE html>
<html ng-app="app">
<head>
    <title></title>
    <script type="text/javascript" src="angular.min.js"></script>
</head>
<body ng-controller="myinfoCtrl">

<p ng-show="on">111</p>

<script type="text/javascript">
    var app = angular.module('app', []);
    app.controller('myinfoCtrl', ['$scope',function($scope) {
        $scope.on=true;
        setTimeout(function(){
            console.log($scope.on)
            $scope.on=false;
            $scope.$apply();
            console.log($scope.on)
        },1000)
    }]);
</script>
</body>
</html>

You need to manually call $scope.$apply();

in the callback function

Or use $timeout
http://www.cnblogs.com/ys-ys/...

Hope to adopt it, thank you

某草草

Use $timeout:

app.controller('myinfoCtrl', ['$scope','$timeout'],function($scope,$timeout) {
    $scope.on=true;
    $timeout(function(){
        $scope.on=false;
    },1000)
}

Changing the variables bound in $scope directly in setTimeout will not trigger the dirty data check of the variables. Variable changes will not be synchronized to the interface, so the interface will not make changes.

PHPzhong

I have never used setTimeout, but changing setTimeout to $timeout can achieve the effect you mentioned

洪涛

<p ng-show="on">111</p>

app.controller('myinfoCtrl', ['$scope'],function($scope) {

$scope.on=true;
setTimeout(function(){
    $scope.$apply(function(){
        $scope.on=false;
    });
},1000)

}
Using setTimeout will not trigger the $digest loop. It is recommended to use the $timeout packaged in angular

世界只因有你


@ Meteor Stay Why does it report an error?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template