2017-10-05 117 views
0

我想功能一个运行过程中和完成后,显示一个微调到按钮,隐藏微调,这是我的尝试:

<div ng-controller="MyController">  
     <button ng-click="InsertData()"> 

      <i class="fa fa-refresh fa-spin" ng-show="loading"></i>Loading 
     </button> 
     <br /> 
     {{loading}} 
    </div> 

和这是controller.js

angular.module('MyApp', []).controller('MyController', function ($scope) { 

$scope.InsertData=function() 
{ 
    $scope.loading = true; 
    $scope.one($scope.two); 

} 

$scope.one = function (callback) { 

    setTimeout(function() { alert("this is function one"); callback(); }, 1000); 

} 

$scope.two = function() { 
    alert("two"); 
    $scope.loading = false; 

} 

}); 

,但此行

$scope.loading = false; 

不执行!虽然它上面运行,我的意思是警报(“两个”)出现!

为什么$ scope.loading的值在回调函数中没有改变?如何解决这个问题呢?

回答

0

问题是因为您使用的是setTimeout这是一个Javascript函数,这会导致问题,其中范围变量不会在范围内更新。因此,我们必须使用这种角度包装,这是$timeout()

更多细节here,要知道其中的差别$timeout()setTimeout()之间是指here


下面是此问题的基本工作提琴。

JSFiddle Demo