我正在尝试使用带有表达式跟踪的 ng-repeat 指令来显示单选按钮,当我提交值时附加在模型中并且当我使用模型中的值重新打开页面时,单选按钮不显示检查.
我已经实现了平面字符串模型 + 字符串值.但这次我尝试使用对象.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script><div ng-app="app" ng-controller="MyCtrl"><表格名称="myForm"><p>新尝试</p><ul><li ng-repeat="i in peopleNew.person"><标签>{{一世}}JS代码
angular.module('app', []).controller('MyCtrl', ($scope) => {$scope.peopleNew ={人: {名称":林戈",id":R",主题":科学"}}$scope.peopleServer= {人:{姓名":林戈"}}});
如上所述,我应该在屏幕上有 4 个单选按钮,我可以选择 1 并提交.然后当我再次在我的模型中打开它时,该人具有正确的值,该值已通过 ng-value
保存但仍在 UI 上,我没有看到单选按钮已检查名称 Ringo 应该被检查.型号有:
$scope.peopleServer= {人:{姓名:Ringo"}}
尝试过的解决方案
- ng-checked 表达式,虽然我读过 ng-model 和 ng-checked不应一起使用,理想情况下应使用模型绑定进行检查.
- 解释我读到,ng-repeat 无法正确渲染,所以我尝试强制重新渲染但没有成功.
- 从模板中移除 ng-checked 仍然无效.
- Track by 适用于 ng-repeat 中的字符串值.在 ng-options 中,它也适用于对象值,但它不是输入元素,而是选择元素
有人帮助理解,当您重新加载或您已经拥有模型中的值时,如何选择单选按钮
angular.module('app', []).controller('MyCtrl', ($scope) => {$scope.peopleNew ={人: {名称":林戈",id":R",主题":科学"}}//取消注释以进行测试.$scope.peopleServer= {人:{姓名":林戈"}}});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></脚本><div ng-app="app" ng-controller="MyCtrl"><表格名称="myForm"><p>新尝试</p><ul><li ng-repeat="i in peopleNew.person"><标签>{{一世}}
自动?我上面的所有尝试都不起作用我错过了什么.
解决方案 你有一些语法错误,缺少模型值和一些不必要的标记.首先,您可以完全摆脱 ng-checked
.如果您正确设置了 ng-model
和 ng-value
,这将自动处理.由于您的对象是独一无二的,因此不需要 track by
.
当使用 AngularJS 指令(例如 ng-value
)时,你不需要使用大括号来引用你的模型值.所以 ng-value="{{person}}"
变成了 ng-value="person"
.
您引用了 myObj.person
,但似乎没有对应的模型值.下面是您提供的代码和标记的编辑,表明使用对象作为单选按钮的值确实有效.
angular.module('app', []).controller('MyCtrl', ($scope) => {$scope.people = [{名称:约翰",编号:J"}, {名称:保罗",编号:P"}, {名称:乔治",编号:G"}, {名称:林戈",编号:R"}];$scope.myObj = {人:$scope.people[1]};});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></脚本><div ng-app="app" ng-controller="MyCtrl"><表格名称="myForm"><p>决定</p><ul><li ng-repeat="人中人"><标签>{{ 人名 }}{{ myObj.person }}</div></div>
I am trying to use ng-repeat directive with track by expression, to show radio buttons, when I submit the value gets attached in the model and when I reopens the page using the values in model, the radio button does not show checked.
I have implemented same with plane string model + string values . But this time am trying with objects.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
<form name="myForm">
<p>New TRY</p>
<ul>
<li ng-repeat="i in peopleNew.person">
<label>
{{i}}
<input type="radio" ng-model="peopleServer.person"
name="same" ng-value="i" />
</label>
</li>
</ul>
</form>
<div>
JS code
angular.module('app', [])
.controller('MyCtrl', ($scope) => {
$scope.peopleNew ={
person: {
"name": "Ringo",
"id": "R",
"subj": "Sci"
}
}
$scope.peopleServer= {
person: {"name":"Ringo"}
}
});
As per above, I should have 4 radio buttons on the screen, I am able to select 1 and submit. And then when I again open it in my model the person have the right value which was saved thorough ng-value
but still on UI i don't see the radio button as checked for name Ringo should be checked. Model have:
$scope.peopleServer= {
person: {name:"Ringo"}
}
Tried Solutions
- ng-checked expression , although I read that ng-model and ng-checked
should not be used together, ideally using model binding it should be chcked.
- explanationI read about ,ng-repeat is not rendering properly so i tried to re render forcefully but did not work.
- Removed ng-checked from the template still did not work.
- Track by works for string values in ng-repeat.In ng-options it worked for object values also, but then its not input element but a select element
Someone help understand, when you reload or you already have the value in model,how the radio button be selected
angular.module('app', [])
.controller('MyCtrl', ($scope) => {
$scope.peopleNew ={
person: {
"name": "Ringo",
"id": "R",
"subj": "Sci"
}
}
//uncomment for testing.
$scope.peopleServer= {
person: {"name":"Ringo"}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
<form name="myForm">
<p>New TRY</p>
<ul>
<li ng-repeat="i in peopleNew.person">
<label>
{{i}}
<input type="radio" ng-model="peopleServer.person"
name="same" ng-value="i" />
</label>
</li>
</ul>
</form>
<div>
automatically ? all my tries above are not working am i missing something.
解决方案 You have some syntax errors, missing model values and some unnecessary markup. First, you can get rid of the ng-checked
altogether. That will be handled automatically if you set your ng-model
and ng-value
properly. Since your objects are unique there's no need for track by
.
When using AngularJS directives (such as ng-value
) you do not need to use braces to reference your model values. So ng-value="{{person}}"
becomes ng-value="person"
.
You refer to myObj.person
, but there doesn't appear to be a corresponding model value. Below is an edit of the code and markup you have provided showing that it really does work to use an object as the value for your radio buttons.
angular.module('app', [])
.controller('MyCtrl', ($scope) => {
$scope.people = [{
name: "John",
id: "J"
}, {
name: "Paul",
id: "P"
}, {
name: "George",
id: "G"
}, {
name: "Ringo",
id: "R"
}];
$scope.myObj = {
person: $scope.people[1]
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
<form name="myForm">
<p>Decisions</p>
<ul>
<li ng-repeat="person in people">
<label>
{{ person.name }}
<input type="radio" ng-model="myObj.person"
name="sameName" ng-value="person" />
</label>
</li>
</ul>
</form>
<div>
{{ myObj.person }}
</div>
</div>
这篇关于AngularJs:ng-model 未绑定到 ng-checked 输入类型 =“radio",与 ng-repeat 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
相关文章