我需要在按下按钮时隐藏“可见块”,但片刻之后再次出现,而不按下按钮。这个版本不起作用。
<!DOCTYPE html>
<html ng-app="app">
<script src="angular.min.js"></script>
<body ng-controller="MyController as ctrl">
<div ng-show="isShow">Visible block</div>
<button ng-click="ctrl.change()">Hide Block</button>
</body>
<script>
var app = angular.module('app', []);
app.controller('MyController', function ($scope) {
$scope.isShow = true;
this.change = function() {
$scope.isShow = false;
setTimeout(function() {
$scope.isShow = true;
}, 1000)
}
});
</script>
</html>
答案 0 :(得分:0)
你需要使用Angular的$timeout
代替通常的setTimeout
,如下所示:
$timeout(function() {
$scope.isShow = true;
}, 1000)
这是因为setTimeout
在$digest
之外工作; Angular的包装解决了这个问题。