Javascript AngularJS ng型号和ng选中单选按钮滑块
当选中单选按钮时,我正在尝试更新ng模型。虽然我的单选按钮实际上是图像,而且我的单选按钮正在通过使用箭头循环的功能进行更新 由于某些原因,我的$scope.transaction仅在实际单击图像时更新,而不是使用箭头更改图像。知道为什么吗 HTMLJavascript AngularJS ng型号和ng选中单选按钮滑块,javascript,angularjs,Javascript,Angularjs,当选中单选按钮时,我正在尝试更新ng模型。虽然我的单选按钮实际上是图像,而且我的单选按钮正在通过使用箭头循环的功能进行更新 由于某些原因,我的$scope.transaction仅在实际单击图像时更新,而不是使用箭头更改图像。知道为什么吗 HTML <div ng-repeat="contact in contacts" ng-show="showContactID == {{contact.contact_id}}"> <div class="radio
<div ng-repeat="contact in contacts"
ng-show="showContactID == {{contact.contact_id}}">
<div class="radio text-center">
<label>
<input type="radio"
value="{{contact.contact_id}}"
ng-checked="showContactID == {{contact.contact_id}}"
ng-model="transaction.contact_id">
<img src="public/img/profiles/{{contact.contact_id}}.jpg"
class="img-circle"
height="150">
<h4>{{contact.contact_name}}</h4>
</label>
</div>
</div>
<div class="row text-center">
<div class="col-xs-6">
<a class="btn"
ng-click="changeContact('prev')">
<i class="fa fa-chevron-left fa-2x"></i></a>
</div>
<div class="col-xs-6">
<a class="btn"
ng-click="changeContact('next')">
<i class="fa fa-chevron-right fa-2x"></i></a>
</div>
</div>
JavaScript
$scope.changeContact = function(which){
var currentContactID = $scope.showContactID;
var newContactID = 0;
if(which){
angular.forEach($scope.contacts, function(contact, key){
if(contact.contact_id == currentContactID){
if($scope.contacts[which == 'next' ? key + 1 : key - 1] == undefined){
newContactID = $scope.contacts[which == 'next' ? 0 : $scope.contacts.length - 1].contact_id;
}
else{
newContactID = $scope.contacts[which == 'next' ? key + 1 : key - 1].contact_id;
}
}
});
$scope.showContactID = newContactID;
}
};
无论何时调用
changeContact
函数,都要更新transaction.contact\u id
值,并在同一函数中使用newContactID
。transaction.contact\u id做什么?无论如何。这里的问题是,您正在尝试将多个输入值绑定到控制器中的单个值。我试图给你一个例子解决方案,但正如前面所说。我不知道交易的目的。contact_id transaction.contact_id是我数据库中的一个外键,它将许多交易链接到一个联系人。谢谢@shivali。工作得很有魅力!虽然我不确定这是否只是一个快速修复?当联系人更改时,应更新模型,因为我们正在将单选按钮更新为“已选中”。或者我在这里的想法有误吗?否,更改已选中属性时无法更新模型值。为此,您必须更新模型值,并选择与模型值具有相同值的单选按钮。如果从中删除选中的属性,它仍然可以正常工作。为了更好的理解,请看这个例子。谢谢你。非常有用!
$scope.changeContact = function(which){
var currentContactID = $scope.showContactID;
var newContactID = 0;
if(which){
angular.forEach($scope.contacts, function(contact, key){
if(contact.contact_id == currentContactID){
if($scope.contacts[which == 'next' ? key + 1 : key - 1] == undefined){
newContactID = $scope.contacts[which == 'next' ? 0 : $scope.contacts.length - 1].contact_id;
}
else{
newContactID = $scope.contacts[which == 'next' ? key + 1 : key - 1].contact_id;
}
}
});
$scope.showContactID = newContactID;
}
};