2013-03-09 3 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> 

그래서 버튼은 true 또는 false입니다 customer.Tracking 값으로 데이터 바인딩 된 클래스가 있습니다. 버튼을 클릭하면 startTrackingCustById() 메서드가 성공적으로 호출되고 customers 객체의 customer 객체는 customer.Tracking = true처럼 성공적으로 변경됩니다.

그러나 단추 클래스는 업데이트되지 않습니다. 내가 뭘 놓치고 있니?

+1

'ng-model = "Id"'은 무엇을 의미합니까? – SET

+0

'class = "tracking - {{customer.Tracking}}' –

답변

1

ng-class을 사용하여 찾습니다. 범위에서 부울 값에 대해 다음을 사용 :

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

을 내가 전화를 싸서 CustCtrl에서 나는이 때 연결됩니다 답변의 제안에 따라이

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

같은 고객의 배열을 업데이트하는 것이 나는 기본적으로 뷰를 업데이트하는 데 문제가있는 경우가 대부분 $ 범위를 사용합니다 "말했다 그것을 찾을 수 있습니다. $ 적용

그래서 일입니다. 지금은 왜, 어떻게 알아 내야 얻을.

+0

Angular의"바깥 "에있는 경우 $ apply()가 필요합니다. 예를 들어 Angular의 $를 사용하지 않는 경우 http 서비스를 사용하지만 데이터를 얻기위한 다른 AJAX 서비스 인 Angular는 비 앵글 콜백에서 $ scope 속성을 업데이트하면 알 수 없습니다. 이러한 경우에는 $ apply가 필요합니다. –

관련 문제