当使用 ng-repeat 创建时,ng-change 事件只为每个单选按钮触发一次 [英] ng-change event only fires once for each radio button when created with ng-repeat 查看:29 发布时间:2021/11/9 22:47:33 angularjs angularjs-directive 本文介绍了当使用 ng-repeat 创建时,ng-change 事件只为每个单选按钮触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我创建了一个带有单选按钮的 AngularJS 指令来控制我的页面将查询的环境并将其添加到我的页面中.我使用双向绑定将名为 environment 的局部范围变量映射到具有相同名称的应用程序变量.当我在模板中显式创建单选按钮时,它似乎工作得很好(在我的实际代码中,我使用 templateUrl 代替,但仍然有同样的问题).<label><input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(1)" value="1"/>测试</label><label><input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(2)" value="2"/>Production</label>我可以根据需要多次选择每个选项,并且值会一直冒泡到应用的范围变量.我想更改单选按钮的创建以在选择对象数组上使用 ng-repeat.它创建选项,并捕获 ngChange 事件,但每个选项只捕获一次.<input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(choice)" value="{{choice.id }}"/>{{choice.name }}标签>这是工作版本小提琴和我的指令代码的相关部分:模板:''+ '<label><input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(1)" value="1"/>测试</label>'+ '<label><input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(2)" value="2"/>Production</label>'+ '<div>指令环境:{{环境}}</div>'+ '</div>',链接:功能(范围,元素,属性){scope.changeEnvironment = function(choiceID) {console.log('SELECTED environment' + choiceID);范围.环境=选择ID;console.log('指令环境 = ' + scope.environment);};}还有这里是只运行一次的版本:template: ''+ '<input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(choice)" value="{{ choice.id }}"/>{{ choice.姓名 }}'+ '</标签>'+ '<div>指令环境:{{环境}}</div>'+ '</div>',链接:功能(范围,元素,属性){范围.选择 = [{ id: 1, name: "Testing" },{ id: 2, name: "Production" }];scope.changeEnvironment = 函数(选择){console.log('SELECTED environment' + choice.id);范围.环境 = 选择.id;console.log('指令环境 = ' + scope.environment);};}我是 AngularJS 的新手,所以我完全有可能犯了一个非常基本的错误.有人能指出我正确的方向吗?UPDATE 根据 callmekatootie 的建议,我将相关事件从 ng-change 更改为 ng-click,并且每次都会触发.这暂时可以作为一种解决方法,但我最初使用 ng-change 是因为我认为 ng-click 不会适用于单击文本标签而不是输入本身,但实际上确实如此.仍然不明白为什么 ng-change 只触发一次. 解决方案 使用对象代替原始值:app.controller('mainController', ['$scope', function (scope) {scope.environment = { 值:'' };}]);删除 ng-change 和 $watch.ng-model 就足够了(除非我误解了用例).演示: http://jsfiddle.net/GLAQ8/可以找到关于原型继承的非常好的帖子 此处.I have created an AngularJS directive with radio buttons to control which environment my page will query against and added it to my page. I am using a two-way binding to map a local scope variable called environment to an app variable with the same name. It seems to work well when I create the radio buttons explicitly in the template (in my actual code I'm using templateUrl instead, but still have the same problem). <div> <label><input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(1)" value="1" />Testing</label> <label><input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(2)" value="2" />Production</label> </div> I can select each choice as many times as I want, and the value bubbles all the way up to the app's scope variable. I wanted to change the creation of the radio buttons to use ng-repeat on an array of choice objects. It creates the options, and it captures the ngChange event, but only once for each choice.<label ng-repeat="choice in choices"> <input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(choice)" value="{{ choice.id }}" />{{ choice.name }} </label> Here is the working version fiddle and the relevant parts of my directive code:template: '<div>' + '<label><input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(1)" value="1" />Testing</label>' + '<label><input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(2)" value="2" />Production</label>' + '<div>Directive environment: {{ environment }}</div>' + '</div>', link: function(scope, element, attributes) { scope.changeEnvironment = function(choiceID) { console.log('SELECTED environment ' + choiceID); scope.environment = choiceID; console.log('directive environment = ' + scope.environment); }; } And here is the version that only works once:template: '<div><label ng-repeat="choice in choices">' + '<input type="radio" name="env" ng-model="environment" ng-change="changeEnvironment(choice)" value="{{ choice.id }}" />{{ choice.name }}' + '</label>' + '<div>Directive environment: {{ environment }}</div>' + '</div>', link: function(scope, element, attributes) { scope.choices = [ { id: 1, name: "Testing" }, { id: 2, name: "Production" } ]; scope.changeEnvironment = function(choice) { console.log('SELECTED environment ' + choice.id); scope.environment = choice.id; console.log('directive environment = ' + scope.environment); }; } I'm brand-new to AngularJS, so it's entirely possible I'm making a very basic mistake. Can anyone point me in the right direction? UPDATE As per callmekatootie's suggestion, I changed the event in question from ng-change to ng-click, and it fires every time. That will do as a work-around for now, but I originally used ng-change because I didn't think ng-click would apply to changes caused by clicking on the text label rather than the input itself, but in fact it does. Still don't get why ng-change only fires once, though. 解决方案 Use an object instead of a primitive value:app.controller('mainController', ['$scope', function (scope) { scope.environment = { value: '' }; }]); Remove ng-change and $watch. ng-model will be enough (unless I'm misunderstanding the use case). Demo: http://jsfiddle.net/GLAQ8/ Very good post on prototypal inheritance can be found here. 这篇关于当使用 ng-repeat 创建时,ng-change 事件只为每个单选按钮触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 查看全文 相关文章 ng-change事件使用ng-repeat创建时,只会为每个单选按钮触发一次; 使用 ng-repeat 时默认选中单选按钮; 使用ng-repeat时默认选中单选按钮; AngularJS单选按钮ng-repeat值; ng-checked和ng-change单选按钮无法一起使用-angularjs; AngularJs 单选按钮在使用 ng-form 和 ng-repeat 时连接; AngularJs 单选按钮在使用 ng-form 和 ng-repeat 时连接; 输入空格时如何触发 ng-change?; Angular.js - 当 ng-pattern 为 $invalid 时,ng-change 不会触发; AngularJS ng-click 事件未在 ng-repeat 中触发; AngularJS ng-click 事件未在 ng-repeat 中触发; 当一个单选按钮在 ng-repeat 中时,当我点击单选按钮时,div 将被启用?; 当一个单选按钮在 ng-repeat 中时,当我点击单选按钮时,div 将被启用?; 当一个单选按钮在 ng-repeat 中时,当我点击单选按钮时,div 将被启用?; 如何从jquery触发ng-change?; 如何从 jquery 触发 ng-change?; 如何在输入空格时触发ng-change?; ng-repeat 里面的 ng-repeat; ng-repeat 里面的 ng-repeat; 为ng-repeat单选按钮选择了错误的单选按钮; ng-checked 和 ng-change 单选按钮不能一起工作 - angularjs; AngularDart ng-change; AngularDart ng-change; 如何将ng-repeat放入ng-repeat中n次; ng-repeat 内的 ng-form,ng-repeat 外的提交按钮; 其他开发最新文章 拒绝显示一个框架,因为它将'X-Frame-Options'设置为'sameorigin'; 什么是&QUOT; AW&QUOT;在部分标志属性是什么意思?; 在运行npm install命令时获取'npm WARN弃用'警告; cmake无法找到openssl; 从Spark的scala中的* .tar.gz压缩文件中读取HDF5文件; Twitter :: Error :: Forbidden - 无法验证您的凭据; 我什么时候需要一个fb:app_id或者fb:admins?; 将.db文件导入R; npm通知创建一个lockfile作为package-lock.json。你应该提交这个文件; 拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src'self'”; 热门教程 Java教程 Apache ANT 教程 Kali Linux教程 JavaScript教程 JavaFx教程 MFC 教程 Apache HTTP客户端教程 Microsoft Visio 教程 热门工具 Java 在线工具 C(GCC) 在线工具 PHP 在线工具 C# 在线工具 Python 在线工具 MySQL 在线工具 VB.NET 在线工具 Lua 在线工具 Oracle 在线工具 C++(GCC) 在线工具 Go 在线工具 Fortran 在线工具