AngularJs:ng-model 未绑定到 ng-checked for input type="radio",与 ng-repeat 一起使用 [英] AngularJs : ng-model not binding to ng-checked for input type="radio", using with ng-repeat

查看:29
本文介绍了AngularJs:ng-model 未绑定到 ng-checked for input type="radio",与 ng-repeat 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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"><标签>{{一世}}<input type="radio" ng-model="peopleServer.person"name="same" ng-value="i"/></表单><div>

JS代码

 angular.module('app', []).controller('MyCtrl', ($scope) => {$scope.peopleNew ={人: {"name": "林戈","id": "R","subj": "科学"}}$scope.peopleServer= {人:{"name":"Ringo"}}});

如上所述,我应该在屏幕上有 4 个单选按钮,我可以选择 1 并提交.然后当我再次在我的模型中打开它时,该人具有正确的值,该值已完全保存 ng-value 但仍然在 UI 上我没有看到单选按钮,因为应检查名称 Ringo.型号有:

 $scope.peopleServer= {人:{姓名:林戈"}}

尝试过的解决方案

  1. ng-checked expression ,虽然我读过 ng-model 和 ng-checked不应该一起使用,理想情况下使用模型绑定应该检查它.
  2. 说明我读到,ng-repeat 渲染不正确,所以我尝试强制重新渲染但没有用.
  3. 从模板中删除 ng-checked 仍然无效.
  4. Track by 适用于 ng-repeat 中的字符串值.在 ng-options 中它也适用于对象值,但它不是输入元素而是选择元素

有人帮助理解,当你重新加载或模型中已经有值时,如何选择单选按钮

 angular.module('app', []).controller('MyCtrl', ($scope) => {$scope.peopleNew ={人: {"name": "林戈","id": "R","subj": "科学"}}//取消注释以进行测试.$scope.peopleServer= {人:{"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"><表单名称="myForm"><p>新尝试</p><ul><li ng-repeat="i in peopleNew.person"><标签>{{一世}}<input type="radio" ng-model="peopleServer.person"name="same" ng-value="i"/></表单>

自动?我上面的所有尝试都不起作用,是我遗漏了什么.

解决方案

您有一些语法错误、缺少模型值和一些不必要的标记.首先,您可以完全摆脱 ng-checked.如果您正确设置 ng-modelng-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"}, {name: "林戈",编号:R"}];$scope.myObj = {人:$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"><表单名称="myForm"><p>决定</p><ul><li ng-repeat="person in people"><标签>{{ 人名}}<input type="radio" ng-model="myObj.person"name="sameName" ng-value="person"/></表单><div>{{ myObj.person }}

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

  1. 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.
  2. explanationI read about ,ng-repeat is not rendering properly so i tried to re render forcefully but did not work.
  3. Removed ng-checked from the template still did not work.
  4. 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 for input type="radio",与 ng-repeat 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆