2014-02-16 22 views
0

我有一個包含多個ID的作用域。我需要找到所有帶有這些ID的按鈕(通過ng-repeat生成),禁用它們並替換爲新文本。角度:通過作用域中的ID禁用所有按鈕

典型的HTML標記:

<li ng-repeat="friend in friends"> 
<strong>{{friend.first_name}} {{friend.last_name}}</strong> 
<button type="button" id="{{friend.uid}}-btn" class="btn btn-love" data-toggle="modal" data-target="#{{friend.uid}}-post">Send</button> 
</li> 

需要更換發送已經發出已和禁用按鈕。

這怎麼可能?

+0

你看着用['ngDisabled'(http://docs.angularjs.org/api/ng.directive:ngDisabled)? – JohnnyHK

+0

是的,我發現了。但我不知道如何用我的任務來實現它。 – Alex

+4

嘗試使用數據綁定而不是DOM操作實現此目的... –

回答

1

將屬性添加到您的$ scope模型中,告訴您是否發送了電子郵件/消息/心臟。這將是在你的控制器:

$scope.friends.sent = false; 

如果你是從一個RESTful源獲取的朋友,當您執行$http.get(),您可以通過循環的結果返回給回調之前,並添加此屬性。

$http.get(url, config) 
    .success(function(data, status, headers, config) { 
     // data will contain your results 
     angular.forEach(values, function(value, key) { 
      // Add properties and default values needed by the controller 
      this.sent = false; 
     }, data); 
     callback(data); 
    }); 

我跳過了處理上面例子的錯誤情況。您需要將其添加到生產代碼中。

那麼你的HTML可以成爲這樣的事情:

<li ng-repeat="friend in friends"> 
<strong>{{friend.first_name}} {{friend.last_name}}</strong> 
<button type="button" id="{{friend.uid}}-btn" class="btn btn-love" 
     data-toggle="modal" data-target="#{{friend.uid}}-post" 
     ng-disabled="friend.sent" 
     >{{ (friend.sent) ? "Sent" : "Send" }}</button> 
</li>