如何使用angularjs在延迟后更改值? [英] How to change value after delay by using angularjs?

查看:20
本文介绍了如何使用angularjs在延迟后更改值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基于 angularjs

创建了基本应用程序

HTML:

<div ng-controller="Ctrl">我的名字是<input type="text"/>瓦尔:{{val}}<br/><button ng-disabled="val">提交</button>

JS:

var app = angular.module('miniapp', []);var glob;功能Ctrl($范围){glob = $scope;$scope.val = false;window.setTimeout(function() {$scope.val = true;}, 3000);}window.setTimeout(function() {glob.val = 真;}, 3000);

如您所见,我尝试在 3 秒后通过 2 种方式将 val 更改为 true 但没有人为我工作.真的很奇怪.我错过了什么吗?

实际上我在收到 Ajax 响应后尝试更改值,但假设应该是同样的问题.

谢谢,

这是我的例子:http://jsfiddle.net/6uKAT/20/

解决方案

尝试使用:$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);       
} 

这篇关于如何使用angularjs在延迟后更改值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆