2013-05-08 49 views
16

我在動態生成angular的ui.bootstrap中的無線電模型選項時遇到問題。我以爲我可以簡單地NG-重複一個數組,使用它是像這樣的BTN-radio屬性內容:Angular UI.Bootstrap的單選按鈕與ng-repeat一樣奇怪

//in the controller 
$scope.radioModel = undefined; 
$scope.radioModelButtons = ["a", "b", "c"]; 

//in the html 
<div class="btn-group" > 
    <button ng-repeat="value in radioModelButtons" 
    class="btn" type="button" ng-model="radioModel" 
    btn-radio="'{{value}}'"> 
     {{value}} 
    </button> 
</div> 

我使用的角度1.1.4和0.3.0 ui.bootstrap。

Here is a jsfiddle of my efforts,正如您所見,單選按鈕獨立工作,不會影響radioModel變量。

謝謝!

回答

25

這是你應該怎麼寫你的標記:

<button ng-repeat="value in radioModelButtons" 
     class="btn" type="button" ng-model="radio.model" 
     btn-radio="value"> 
      {{value}} 
</button> 

和工作的jsfiddle:http://jsfiddle.net/yMLqz/2/

有兩個問題在你的方法:

  • btn-radio應AngularJS使用表達,而不是內插值
  • ng-repeat是肌酸g一個新的範圍,所以你需要考慮到這一點,如果你想綁定到父範圍上定義的值
+0

非常感謝:D我現在在閱讀「範圍原型繼承的細微差別」 – 2013-05-08 15:36:04