如何在事件更改模型时呈现角度视图?

时间:2015-07-02 11:56:10

标签: javascript angularjs view model

我需要在按下按钮时隐藏“可见块”,但片刻之后再次出现,而不按下按钮。这个版本不起作用。

<!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>

1 个答案:

答案 0 :(得分:0)

你需要使用Angular的$timeout代替通常的setTimeout,如下所示:

$timeout(function() {
    $scope.isShow = true;
}, 1000)

这是因为setTimeout$digest之外工作; Angular的包装解决了这个问题。