2013-03-09 16 views
0

我有以下的html。如何讓Angular從控制器內更新Ui

<div ng-controller="CustCtrl"> 
<table class="table table-condensed"> 
    <thead> 
     etc. 
    </thead> 
    <tbody> 
     <tr ng-repeat="customer in customers" data-cust-id="{{customer.Id}}"> 
      <td> 
       <button ng-model="Id" tracking-{{customer.Tracking}} ng-click="startTrackingCustById(customer.Id)"> 
       </button> 
      </td> 
     etc. 
     </tr> 
    </tbody> 
</table> 

所以按鈕有一個數據綁定到customer.Tracking值是真或假的類。當點擊按鈕時,startTrackingCustById()方法被成功調用,客戶對象中的客戶對象成功更改,如customer.Tracking = true。

但按鈕類沒有更新。我錯過了什麼?

+1

什麼是'ng-model =「Id」'? – SET 2013-03-09 22:19:45

+0

看起來你缺少'class':'class =「tracking - {{customer.Tracking}}' – 2013-03-09 22:30:44

回答

1

看看使用ng-class。對於在範圍上一個布爾值,你可以使用:

ng-class="{'classNameToAddIfTrue':customer.Tracking}" 
1

在CustCtrl我的調用包裝,基於在回答中的建議,我將鏈接到時更新客戶陣列這樣

$scope.$apply($scope.customers[i].Tracking = true); 

我覺得,基本上說:「如果你有麻煩更新視圖中,您最有可能需要使用$範圍。$應用

這樣得到的它的工作。現在我需要弄清楚爲什麼和怎樣。

+0

如果你是Angular的」外部「,那麼需要$ apply()例如,如果你沒有使用Angular的$ http服務,但其他一些AJAX服務來獲取你的數據,如果你更新了非角度回調中的某些$ scope屬性,Angular不會注意到 - 這些情況需要$ apply。 – 2013-03-10 00:56:25

相關問題