问题描述
我正在尝试使用带有表达式跟踪的 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 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22
前端开发问题
182
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14
前端开发问题
156
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24
前端开发问题
271
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18
前端开发问题
301
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18
前端开发问题
125
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17
前端开发问题
437
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序




大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)