2017-08-17 60 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,根據需要設置和更新範圍變量。也許你們中的一位大師可以解釋爲什麼對我和其他人來說,這樣可以幫助別人。感謝大家的幫助。