这是我的代码:
<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
block 。父 block 是products in products
。如您所见,每个产品都有 SelectedDeliveryOption
。例如我有两个单选按钮。 Values(option.Method
) - “bronze”
第一个,“silver”
第二个。此值 (ng-model="product.SelectedDeliveryOption"
) 是 "silver"
。但默认情况下未选中单选按钮。我试过:ng-checked = "option.Method == product.SelectedDeliveryOption"
,但对我不起作用。
$scope.initProductsTable = function (products) {
$scope.products = products; }
谁能帮帮我?
最佳答案
确保 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
值与 radio button
中的 value
相等:
检查下面的片段:
(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>
关于javascript - 无法在 Angular.js 中使用 ng-repeat 设置默认选定单选按钮的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38946542/