我无法弄清楚为什么我的简单AngularJS应用无法正常工作。“正在加载…”应该被隐藏,然后“完成!” 1秒后应显示。
的HTML:
<div ng-app> <div ng-controller="TestCtrl"> <div class="text-center" ng-show="loading"> <h1>Loading...</h1> </div> <div class="text-center" ng-show="!loading"> <h1>Done!</h1> </div> </div> </div>
Javascript:
function TestCtrl($scope) { $scope.loading = true; setTimeout(function () { $scope.loading = false; }, 1000); }
您需要告诉angular您更新了var:
function TestCtrl($scope) { $scope.loading = true; setTimeout(function () { $scope.$apply(function(){ $scope.loading = false; }); }, 1000); }
要不就
function TestCtrl($scope, $timeout) { $scope.loading = true; $timeout(function () { $scope.loading = false; }, 1000); }