search

Home  >  Q&A  >  body text

angular.js - 请教一个关于angularjs的小问题

我是angularjs的一个新手,今天在项目中遇到这样的一个问题
我想在1S种以后给p隐藏掉,但是一直没效果,
求解,为什么一直没反应

<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)
}
天蓬老师天蓬老师2744 days ago501

reply all(5)I'll reply

  • 世界只因有你

    世界只因有你2017-05-15 17:11:02

    <!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

    reply
    0
  • 某草草

    某草草2017-05-15 17:11:02

    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.

    reply
    0
  • PHPz

    PHPz2017-05-15 17:11:02

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

    reply
    0
  • PHP中文网

    PHP中文网2017-05-15 17:11:02

    <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

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-15 17:11:02


    @ Meteor Stay Why does it report an error?

    reply
    0
  • Cancelreply