ng-change不起作用,找不到指令

时间:2017-01-30 09:26:18

标签: angularjs angularjs-directive

<select id = "email_templates" name = "email_templates" class="chosen-select input-md form-control" ng-change = "alert('template selected');">
 <option  value="0">No template selected</option>
 <option value="1">Template 1</option>                                                     
</select>

我收到以下错误。

Error: [$compile:ctreq] Controller 'ngModel', required by directive 'ngChange', can't be found!

所有其他角度指令似乎都运行正常。我做错了什么?

4 个答案:

答案 0 :(得分:4)

您需要为选择

设置ng-model
for _, file := range files {
    fmt.Println(file.Mode()) 
}  

答案 1 :(得分:0)

ng-change reuires ng-model所以使用ng-model和ng-change

 <select id = "email_templates" ng-model="selected" name =
     "email_templates" class="chosen-select input-md form-control" ng-
      change = "alert('template selected');">
      <option  value="0">No template selected</option>
      <option value="1">Template 1</option>                                                     
  </select>

答案 2 :(得分:0)

来自ng-change documentation

  

ngChange表达式仅在输入发生变化时进行评估   value会将新值提交给模型。

     

...

     

请注意,此指令要求ngModel存在。

ng-change为您的模型变量创建观察者。因此,您必须在选择中添加ng-model,以告知Angular您观察到的变量发生了变化。

<select ng-model="selectedEmail" ng-change="alert('template selected');">

答案 3 :(得分:0)

<select id = "email_templates" name = "email_templates" class="chosen-select input-md form-control" ng-change = "alert('template selected');">
 <option  value="0">No template selected</option>
 <option value="1">Template 1</option>                                                     
</select>

而不是这个你可以使用ngClick选项。根据这个问题Responding to drop down selection change in angular without model binding

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<select id = "email_templates" name = "email_templates" class="chosen-select input-md form-control" >
 <option  value="0">No template selected</option>
 <option value="1" ng-click="alert('template selected');">Template 1</option>                                                     
</select>