2017-08-17 66 views
-1

我已经看到这个问题很多,但我没有尝试过的解决方案正在为我工​​作。我尝试使用$apply(),这给了我一个错误,指出摘要循环已经在运行。我尝试使用Dot“。”符号,但没有变化。我甚至尝试过使用超时和承诺,但仍不会在视图中更新。Angularjs范围变量不会更新查看

下面是HTML:

<th colspan="4" ng-class="{'ssqtrue': deficiencies === false , 'ssqfalse': deficiencies === true}"> 
<span ng-model="definfo">{{definfo}}</span> 
    </th> 

这里是我的控制器代码:

$scope.recalculateDashboard = function (goToDashboard) { 
    contractorService 
     .calculateScores() 
     .success(function() { 
      getscoringDetails(); 
      getDefInfo(); 
      if (goToDashboard) { 
       $scope.tabs[0].active = true; 
      } 
     }).error(function (reason) { 
      console && console.log(reason.statusText); 
      genericErrorAlertHandler(); 


     }); 
}; 

function getDefInfo() { 
    contractorService.getDeficiencyInfo() 
     .success(function (data) { 
      $scope.$apply(function() { 
       $scope.definfo = data; 
      }); 
      if ($scope.definfo == 'No Deficiencies Found') { 
       $scope.deficiencies = false; 
      } else { 
       $scope.deficiencies = true; 
      } 
     }).error(function (reason) { 
      console && console.log(reason.statusText); 
      genericErrorAlertHandler(); 


     }); 
} 

对于我的生活,我也弄不清是怎么回事的。任何援助非常感谢!

+0

您不能将'ng-model'绑定到'span',并且使用'then'函数而不是'success',因为它不推荐使用。 –

+0

阅读[ng-model和ng-bind]之间的区别(https://stackoverflow.com/a/12420157/5447994) – Thamilan

+0

ng-bind首先出现并且没有工作,所以我将其更改为ng-model –

回答

0

问候<span ng-model="definfo">{{definfo}}</span>

你不需要在这里ng-model指令。足够使用{{definfo}}或更好的办法像人指出使用ng-bind像:

<span ng-bind="definfo"></span> 

我尝试使用$apply()这给了我一个错误

一般情况下,开发人员使用$scope.$apply当从3D党GET回调像jQuery这样的代码不在摘要循环中。所以不要使用它。顺便说一句,安全的方式是使用$timeout例如a.e.包装为:

$timeout(function() { 
    $scope.definfo = data; 
}); 

我相信,如果你不使用无极连锁你可以使用successerror回调(你实际上没有),而不是使用then()


关于$http.get('/SSQV4/SSQV5/Contractor/GetDeficiencyInfo');

$http.get返回原始承诺,但它也返回status 'Ok' ,config,headers ...,我确定你不想在你的控制器中解析完整的响应。 因此,我会在您的服务中创建新的Promise并仅提取结果。

所以不是:return $http.get(URL);我会在服务写:

this.getDeficiencyInfo = function() {   
    var deferred = $q.defer(); 
     $http({method: 'GET', url: URL}).then(function(res){ 
     deferred.resolve(res.data); 
     }, function (error) { 
      console.error(error); 
      deferred.resolve({error:error}); //actually you can reject 
     });  
    return deferred.promise; 
    }; 

DEMO in fiddle

+0

拿来的例子中,我应该说我已经尝试过使用超时以及没有任何区别。我会将其添加到我的描述中。谢谢。 –

+0

我的原始Html是。我认为添加模型可能会有所帮助。 –

+0

@RaniRadcliff更新了我的回答 –

0

我想通了这一点,虽然我不知道 “为什么” 就引起了这个特定的问题。第一次加载页面时$scope.definfo由另一个http调用的变量设置。变量var pagedata = []保存了页面第一次呈现时从服务返回的大量信息。范围变量然后用以下代码设置:$scope.definfo = pagedata.DeficiencyInfo。删除该行并直接调用函数getDefInfo,根据需要设置和更新范围变量。也许你们中的一位大师可以解释为什么对我和其他人来说,这样可以帮助别人。感谢大家的帮助。