javascript - Angularjs - 单选按钮在 ng-repeat 内显示奇怪的 Action

标签 javascript angularjs radio-button angularjs-ng-repeat

我有一个包含对象的数组,来自服务器。

我必须用这个数组制作列表。

我想为每个单选按钮设置默认值,

但只有最后一项有默认值。

请查看我的代码。

    <div id="{{product.product_id}}" class="item-box" ng-repeat="product in cartProducts track by $index">
        <div class="radio-box">
            <input type="radio" name="send_num" ng-checked="product.isBasicNum" ng-click="product.isBasicNum=true"> NumType A
            <input type="radio" name="send_num" ng-checked="!product.isBasicNum" ng-click="product.isBasicNum=false"> NumType B
        </div>
        <div class="radio-box">
            <input type="radio" name="send_res" ng-checked="product.isImmediateSend" ng-click="product.isImmediateSend=true"> SendType A
            <input type="radio" name="send_res" ng-checked="!product.isImmediateSend" ng-click="product.isImmediateSend=false"> SendType B
        </div>
        <div class="radio-box">
            <input type="radio" name="receive" ng-checked="product.isRecieveDupable" ng-click="product.isRecieveDupable=true"> ReceiveType A
            <input type="radio" name="receive" ng-checked="!product.isRecieveDupable" ng-click="product.isRecieveDupable=false"> ReceiveType B
        </div>
    </div>

this is fiddle .

我错过了什么?

我试图找到答案,但找不到与我类似的答案。

如何为所有项目设置默认值?

提前致谢。

最佳答案

所有单选按钮共享相同的三个 name 属性值。所以你错误地将它们分组。这意味着当您单击一个单选按钮(将其设置为 true)时,浏览器会自动将同一组中的所有其他单选按钮设置为 false。

点击 radio 就会告诉你这个事实。您需要使用不同的 name 属性值。请注意我在下面的示例中使用的 {{$index}}:

var myApp = angular.module('MyApp', []);
myApp.controller('MyController', function($scope){
	$scope.cartProducts = [
		{product_id:1291803},
		{product_id:2365123},
		{product_id:5463434},
		{product_id:8752642},
		{product_id:4566484}
	];
	$scope.initDefValue = function () {
        angular.forEach($scope.cartProducts, function (product) {
            product.isBasicNum = true;
            product.isImmediateSend = true;
            product.isDirectEnterNum = true;
            product.isRecieveDupable = true;
            product.isBasicBanner = true;
        });
    };
	$scope.initDefValue();
});
.item-box {
	border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp">
	<div ng-controller="MyController">
		<div id="{{product.product_id}}" class="item-box" ng-repeat="product in cartProducts track by $index">
			<div class="radio-box">
				<input type="radio" name="send_num{{$index}}" ng-checked="product.isBasicNum" ng-click="product.isBasicNum=true"> NumType A
				<input type="radio" name="send_num{{$index}}" ng-checked="!product.isBasicNum" ng-click="product.isBasicNum=false"> NumType B
			</div>
			<div class="radio-box">
				<input type="radio" name="send_res{{$index}}" ng-checked="product.isImmediateSend" ng-click="product.isImmediateSend=true"> SendType A
				<input type="radio" name="send_res{{$index}}" ng-checked="!product.isImmediateSend" ng-click="product.isImmediateSend=false"> SendType B
			</div>
			<div class="radio-box">
				<input type="radio" name="receive{{$index}}" ng-checked="product.isRecieveDupable" ng-click="product.isRecieveDupable=true"> ReceiveType A
				<input type="radio" name="receive{{$index}}" ng-checked="!product.isRecieveDupable" ng-click="product.isRecieveDupable=false"> ReceiveType B
			</div>
		</div>
	</div>
</div>

关于javascript - Angularjs - 单选按钮在 ng-repeat 内显示奇怪的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49869894/

相关文章:

html - 是否可以使用没有标签的纯 css 单选按钮输入进行自定义?

javascript - 为什么我的表达式在页面加载时没有在 Angular 中自动更新?

javascript - jQuery 移动 radio 事件监听器

javascript - 使用 toDataURL() 的多个 Canvas 图像(由 Chart.js 提供)

javascript - 如何用javascript知道输入位置

javascript - AngularJS + IndexedDB,一次打开数据库供所有 Controller 使用,还是为每个请求单独打开?

javascript - Flask 和 Angular Web 应用程序路由

python - 即使我使用 destroy/pack_forget,Tkinter Widget 也不会消失

javascript - 当 URL 不完整时,在新选项卡中打开 URL 会生成 404

javascript - ngIf 和 @Input angular2 奇怪的行为