2016-08-14 27 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

标记为正确的,因为你的答案是非常好的。 –