且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用angularjs在延迟后更改值?

更新时间:2022-10-27 19:07:19

尝试使用:$timeout代码>

Angular 的 window.setTimeout 包装器.fn 函数被包装进入 try/catch 块并将任何异常委托给$exceptionHandler 服务.

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

更新小提琴

JavaScript

var app = angular.module('miniapp', []);功能 Ctrl($scope, $timeout) {$scope.val = false;$timeout(function(){$scope.val = true}, 3000);}

I created basic application based on angularjs

HTML:

<div ng-app="miniapp">
<div ng-controller="Ctrl">
    My name is 
    <input type="text"/>   
    Val: {{val}}
    <br/>
    <button ng-disabled="val">Submit</button>        
</div>    

JS:

var app = angular.module('miniapp', []);

var glob;
function Ctrl($scope) {      
    glob = $scope;    
     $scope.val = false;

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

 window.setTimeout(function() {
            glob.val = true;
        }, 3000); 

As you can see I try to change val to true after 3 sec by 2 ways but no one is working for me. Really strange. Did I miss something?

Actually I try to change value after get response from Ajax, but suppose should be the same problem.

Thanks,

Here is my example: http://jsfiddle.net/6uKAT/20/

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);       
}