How to change value after delay by using angularjs?

后端 未结 2 2064
清歌不尽
清歌不尽 2020-12-13 12:40

I created basic application based on angularjs

HTML:

相关标签:
2条回答
  • 2020-12-13 13:28

    You are making changes to scope outside of what angular knows about (inside a timeout).
    So you should use $timeout.. otherwise you have to use $scope.$apply()

    $timeout(function() {
        $scope.val = true;
    }, 3000); 
    

    http://jsfiddle.net/6uKAT/21/

    For timeout use $timeout and it will call $scope.$apply() for you.
    Likewise, for ajax use $http.

    if you can't use these, then you must call $scope.$apply() yourself:

     window.setTimeout(function() {
         $scope.$apply(function() {
            $scope.val = true;
         });
     }, 3000);
    
    0 讨论(0)
  • 2020-12-13 13:42

    Try using: $timeout

    Angular's wrapper for window.setTimeout. The fn function is wrapped into a try/catch block and delegates any exceptions to $exceptionHandler service.

    $timeout(fn[, delay][, invokeApply]);

    Updated Fiddle

    JavaScript

    var app = angular.module('miniapp', []);
    
    function Ctrl($scope, $timeout) {  
         $scope.val = false;
         $timeout(function(){$scope.val = true}, 3000);       
    } 
    
    0 讨论(0)
提交回复
热议问题