gpt4 book ai didi

javascript - 单击父 div 选择单选按钮并在 angularJS 中获取它的值

转载 作者:太空宇宙 更新时间:2023-11-04 10:44:26 24 4
gpt4 key购买 nike

我想要一个 DIV 表现得像单选按钮,并在 angularJS 中获取它的值。我的 div 看起来像:

<div ng-repeat="address in multipleaddress" class="tileHover widget Text span4">
<div class="radios" for="optionsRadios1{{address.id}}" id="{{address.id}}" ng-model="radiosship.shiphere">
<h3 class="widget-title widget-title" id="shipaddr{{address.id}}">Address #{{$index + 1}}</h3>
<span style="font-size:16px">{{address.firstname}} {{address.lastname}}</span>
<p>{{address.shipping_address}}<br/>{{address.shipping_city}},
<span ngif="{{address.country}} == 'CA'">{{address.shipping_province}}</span>
<span ngif="{{address.country}} == 'US'">{{address.shipping_state}}</span>
<span ngif="{{address.shipping_country}} != 'US' && {{address.shipping_country}} != 'CA'">{{address.otherregion}}</span> - {{address.pincode}},<br/> {{address.country}}</p>
<input type="radio" name="shiphere" id="optionsRadios1{{address.id}}" ng-model="radiosship.shiphere" value="{{address.id}}"/>
</div>

当我点击 radios div 时,该 div 中的单选按钮应该被选中,我希望它在 $scope.radiosship.shiphere 中的值以 Angular 显示。有帮助吗?

最佳答案

我认为这里的主要问题是 DIV 元素不能使用 ng-model 指令——假设您不为此使用自定义指令——。

所以替换<div class="radios" for="optionsRadios1{{address.id}}" id="{{address.id}}" ng-model="radiosship.shiphere">
<div class="radios" for="optionsRadios1{{address.id}}" id="{{address.id}}" ng-click="radiosship.shiphere = address.id; doSomething(address)">

还要将模型值绑定(bind)到 Controller $scope 而不是组件,您可以初始化 radiosship Controller 中的变量如下所示:

$scope.radiosship = {};

这是一个工作示例

   angular.module('App', [])
.controller('MainCtrl', function ($scope) {
$scope.radiosship = {};
$scope.multipleaddress = [
{id: 1, firstname: 'John'},
{id: 2, firstname: 'John'},
{id: 3, firstname: 'John'},
];
});
<!DOCTYPE html>
<html ng-app="App">
<head>
<title>Radio</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-controller="MainCtrl">
<pre>{{radiosship | json}}</pre>
<div ng-repeat="address in multipleaddress" class="tileHover widget Text span4">
<div class="radios" for="optionsRadios1{{address.id}}" id="{{address.id}}" ng-click="radiosship.shiphere = address.id; fetchData(address)">
<h3 class="widget-title widget-title" id="shipaddr{{address.id}}">Address #{{$index + 1}}</h3>
<span style="font-size:16px">{{address.firstname}} {{address.lastname}}</span>
<p>{{address.shipping_address}}<br/>{{address.shipping_city}},
<span ng-if="address.country == 'CA'">{{address.shipping_province}}</span>
<span ng-if="address.country == 'US'">{{address.shipping_state}}</span>
<span ng-if="address.shipping_country != 'US' && address.shipping_country != 'CA'">{{address.otherregion}}</span> - {{address.pincode}},<br/> {{address.country}}</p>
<input type="radio" name="shiphere" id="optionsRadios1{{address.id}}" ng-model="radiosship.shiphere" value="{{address.id}}"/>
</div>
</body>
</html>

关于javascript - 单击父 div 选择单选按钮并在 angularJS 中获取它的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35549519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com