2017-04-09 72 views
0

我想在保存期间禁用提交按钮并在保存后启用它。2方向绑定在角度1中的setTimeout回调中不起作用

它不能启用。我可以看到国旗变了。但view上的标志没有更新,因为我会想象2路绑定应该给我。

的代码如下

$scope.save = function(){ 
    $scope.saving_survey = true; 
    setTimeout(function(){ 
     $scope.saving_survey = false; 
     console.log($scope.saving_survey); 
    }, 1000); 

} 


<button class="btn btn-success" ng-click="save()" ng-disabled="saving_survey"> 
    save {{saving_survey}} 
</button> 
+0

'setTimeout'正在发生变化范围。 ..这就是为什么有一个角度的$超时服务,它包装setTimeout并调用$ apply来运行视图摘要 – charlietfl

回答

0

尝试使用$timeout服务。它应该是injecte到控制器

$scope.save = function(){ 
    $scope.saving_survey = true; 
    $timeout(function(){ 
    $scope.saving_survey = false; 
    console.log($scope.saving_survey); 
    }, 1000); 
} 
0

基本上默认的JavaScript函数(setTimeout的,间隔,事件监听等)没有$看包装。所以手动你需要触发$ watch,因为你需要拨打$scope.$apply()。调用$apply的最佳方式如下图所示,

if(!$scope.$$phase) { 
    $scope.$apply(); 
} 

注:$$phase返回true,如果已经消化别人的进步周期是 将角上下文之外假