2016-08-14 32 views
2

設置使用NG-重複默認選中的單選按鈕的選項,這是我的代碼:不能在Angular.js

<label ng-repeat="option in product.DeliveryOptions" class="radio-inline product__filtr__form__label__input"> 
    <input type="radio" ng-model="product.SelectedDeliveryOption" ng-change="changeDeliveryType(product)" name="inlineRadioOptions{{product.ID}}" 
class="product__filtr__form__radio" value="{{option.Method}}">{{option.Description}} 
</label> 

這是嵌套ng-repeat塊。父塊是product in products。正如你可以看到每個產品都有SelectedDeliveryOption。例如,我有兩個單選按鈕。值爲(option.Method) - "bronze"爲第一個,"silver"爲第二個。該值(ng-model="product.SelectedDeliveryOption")是"silver"。但是默認情況下,單選按鈕沒有被選中。我試過了:ng-checked = "option.Method == product.SelectedDeliveryOption",但不適合我。

$scope.initProductsTable = function (products) { 
      $scope.products = products; } 

任何人都可以幫助我嗎?

+0

javascr在哪裏ipt的代碼? –

+0

請提供js代碼片段 –

+0

只需初始化產品$ scope.initProducts = function(products){$ scope.products = products};和

回答

1

確保如果value對象option.Method在:

<input type="radio" ng-model="product.SelectedDeliveryOption" ng-change="changeDeliveryType(product)" name="inlineRadioOptions{{product.ID}}" 
class="product__filtr__form__radio" value="{{option.Method}}"> 

的格式爲:

option.Method = {"id": "12345", 
      "value": "teste"} 

,並使用ng-value指令;

像波紋管的例子:

當使用radio button你必須做出等於model值和valueradio button

檢查片斷波紋管:

(function(angular) { 
 
    'use strict'; 
 
    angular.module('includeExample', ['ngAnimate']) 
 
    .controller('ExampleController', ['$scope', '$q', 
 
     function($scope, $q) { 
 
     $scope.color = { 
 
     name: 'blue' 
 
     }; 
 
     $scope.specialValue = { 
 
     "id": "12345", 
 
     "value": "green" 
 
     };   
 

 

 
     } 
 
    ]); 
 
})(window.angular);
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.1/angular.min.js"></script> 
 
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.1/angular-animate.js"></script> 
 

 
<body ng-app="includeExample"> 
 
    <div ng-controller="ExampleController"> 
 
    <form novalidate class="simple-form"> 
 
    <label> 
 
    <input type="radio" ng-model="color.name" value="red"> 
 
    Red 
 
    </label><br/> 
 
    <label> 
 
    <input type="radio" ng-model="color.name" ng-value="specialValue"> 
 
    Green 
 
    </label><br/> 
 
    <label> 
 
    <input type="radio" ng-model="color.name" value="blue"> 
 
    Blue 
 
    </label><br/> 
 
    <tt>color = {{color.name | json}}</tt><br/> 
 
    </form> 
 

 
    </div> 
 
</body>

+0

Alvaro,我發現了一個錯誤。非常感謝您的幫助。單選按鈕的名稱是相同的。真的,不知道我是如何錯過它的。非常感謝您的時間。 –

+0

Np,我很高興如果我幫助:) –

+1

標記爲正確的,因爲你的答案是非常好的。 –