javascript 不能在 ngRepeat 中使用 ngChange - Angularjs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27958163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:12:21  来源:igfitidea点击:

Can't use ngChange in ngRepeat - Angularjs

javascriptangularjscheckboxangularjs-ng-repeat

提问by ocajian

I am using ngRepeat to generate a fieldset of checkboxes.

我正在使用 ngRepeat 生成复选框的字段集。

I do this as follows:

我这样做如下:

<fieldset id="country_select" ng-controller="CountriesListController">
        <span ng-repeat="country in countries">
          <label>
            <input type="checkbox" id="country.name" ng-change="locationAdd(country.name)">
            {{country.name}}
          </label><br/>
        </span>
</fieldset>

However for some reason adding the ngChange directive to my checkbox causes my ng-repeat output to display only one checkbox as follows:

但是由于某种原因,将 ngChange 指令添加到我的复选框会导致我的 ng-repeat 输出仅显示一个复选框,如下所示:

checkbox{{country.name}}

复选框{{country.name}}

When I change ngChange to ngClick my ngRepeat loop works.

当我将 ngChange 更改为 ngClick 时,我的 ngRepeat 循环有效。

So my question is: Why does ngChange not work with ngRepeat but ngClick does when inserted into my checkbox input

所以我的问题是:为什么 ngChange 不能与 ngRepeat 一起使用,而 ngClick 在插入到我的复选框输入中时却可以

I get the following error in my console:

我在控制台中收到以下错误:

Error: [$compile:ctreq] Controller 'ngModel', required by directive 'ngChange', can't be found!
http://errors.angularjs.org/1.3.7/$compile/ctreq?p0=ngModel&p1=ngChange
at REGEX_STRING_REGEXP (http://localhost:9000/bower_components/angular/angular.js:63:12)
at getControllers (http://localhost:9000/bower_components/angular/angular.js:7535:19)
at nodeLinkFn (http://localhost:9000/bower_components/angular/angular.js:7724:33)
at compositeLinkFn (http://localhost:9000/bower_components/angular/angular.js:7073:13)
at compositeLinkFn (http://localhost:9000/bower_components/angular/angular.js:7076:13)
at compositeLinkFn (http://localhost:9000/bower_components/angular/angular.js:7076:13)
at publicLinkFn (http://localhost:9000/bower_components/angular/angular.js:6952:30)
at $get.boundTranscludeFn (http://localhost:9000/bower_components/angular/angular.js:7091:16)
at controllersBoundTransclude (http://localhost:9000/bower_components/angular/angular.js:7747:18)
at ngRepeatAction (http://localhost:9000/bower_components/angular/angular.js:24524:15)angular.js:11592 (anonymous function)angular.js:8542 $getangular.js:14241 $get.Scope.$digestangular.js:14486 $get.Scope.$applyangular.js:9644 doneangular.js:9834 completeRequestangular.js:9775 requestLoaded

回答by Shripal Soni

ng-change will require ng-model. You are missing ng-model on checkbox

ng-change 将需要ng-model. 您在复选框中缺少 ng-model

Check this plunkerfor working example.

检查此plunker以获取工作示例。

回答by Zarepheth

Be careful that you intend to use ngChange. In my case, I was a bit too quick with the fingers and eyes and didn't catch Intellisense choosing data-ng-changewhen I intended to use data-ng-click

请注意您打算使用ngChange. 在我的情况下,我的手指和眼睛有点太快了,并且data-ng-change在我打算使用的时候没有抓住 Intellisense 的选择data-ng-click

I'm pretty sure I only saw one entry (data-ng-click) after typing data-ng-c; but maybe Intellisense was a bit slow in displaying the other options and picked the alphabetically first when I hit [Tab]...

我很确定我在输入后只看到了一个条目(data-ng-click)data-ng-c;但也许 Intellisense 在显示其他选项时有点慢,当我点击 [Tab] 时,首先按字母顺序选择了......