[英]What wrong with my js code?
I tried to send data from JavaScript to html with Angular... I have this code 我试图使用Angular将数据从JavaScript发送到html ...我有这段代码
phonecatControllers.controller('start', ['$scope', function($scope){
$scope.lloadd=true;
console.log('data - '+$scope.lloadd);
function second_passed() {
$scope.lloadd=false;
console.log('data - '+$scope.lloadd);
alert('sdasd');
}
setTimeout(second_passed, 3000);
}]);
And this HTML code 而这个HTML代码
<div ng-show="lloadd">
12312312
</div>
When I start app DIV is visible...but after 3 sec i see only alert... This is my log 当我启动应用程序DIV时可见...但是3秒钟后,我仅看到警报...这是我的日志
01-08 16:34:25.608: I/chromium(22042): [INFO:CONSOLE(179)] "data - true", source: file:///android_asset/www/js/controllers.js (179)
01-08 16:34:28.613: I/chromium(22042): [INFO:CONSOLE(182)] "data - false", source: file:///android_asset/www/js/controllers.js (182)
You must use angularjs $timeout instead of setTimeout
. 您必须使用angularjs $ timeout而不是
setTimeout
。 With setTimeout
angular doesn't get notified that something changed, but with $timeout
it does. 使用
setTimeout
不会通知angular发生了更改,但是使用$timeout
可以通知。
But you can also use $scope.$apply()
in your second_passed()
, but I do not recommend that. 但是您也可以在
second_passed()
使用$scope.$apply()
second_passed()
,但我不建议这样做。
The problem is you're using setTimeout
to cause a change to angular data on the $scope
that angular doesn't know about. 问题是您正在使用
setTimeout
来导致角度不知道的$scope
上的角度数据发生变化。
You need to read about angular's $digest
cycle , but in short, you need to tell angular that some event has happened. 您需要阅读angular的
$digest
循环 ,但是总之,您需要告诉angular一些事件已经发生。
For timeouts, angular provides a service that does this for you, appropriately named $timeout
, so it's very simple: 对于超时,angular提供了一个为您执行此服务的服务,名为
$timeout
,因此非常简单:
phonecatControllers.controller('start', function($scope, $timeout){
$scope.lloadd=true;
console.log('data - '+$scope.lloadd);
function second_passed() {
$scope.lloadd=false;
console.log('data - '+$scope.lloadd);
alert('sdasd');
}
$timeout(second_passed, 3000);
});
Of course be sure to add $timeout
to the arguments to your controller function to ensure it gets injected. 当然,请确保将
$timeout
添加到控制器函数的参数中,以确保注入了它。
Note that you can achieve the same result using $scope.$apply()
to trigger a digest inside your second_passed
function: 请注意,您可以使用
$scope.$apply()
在second_passed
函数内触发摘要来实现相同的结果:
function second_passed() {
$scope.$apply( function () {
$scope.lloadd=false;
});
console.log('data - '+$scope.lloadd);
alert('sdasd');
}
But this is not advisable for a couple of reasons: 但这是不可取的,原因有两个:
$scope.$apply()
can cause problems if there is another digest in progress already, so it's generally best to avoid it. $scope.$apply()
手动触发摘要可能会引起问题,因此通常最好避免这种情况。 $timeout
adds more than just a $scope.$apply
wrapper - it also returns a promise, and has a handy flush()
decorator when you're testing. $timeout
增加了$scope.$apply
包装器-还返回了一个promise,并且在测试时有一个方便的flush()
装饰器。 Whether you are in the habit of using promises and writing tests or not, you should use $timeout
, because someday you will want to use those features. 无论您是否习惯使用promise和编写测试,都应该使用
$timeout
,因为总有一天您会想要使用这些功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.