简体   繁体   English

ng-change不适用于ng-select

[英]ng-change not working on ng-select

I'm using a select box that is populated by the ng-options . 我正在使用由ng-options填充的选择框。 Unfortunately, I cannot get my ng-change function to call. 不幸的是,我无法调用我的ng-change功能。

Here is my Fiddle 这是我的小提琴

Here is my js : 这是我的js

var myApp = angular.module('myApp',[]);


function MyCtrl($scope) {

    $scope.typeOptions = [
    { name: 'Feature', value: 'feature' }, 
    { name: 'Bug', value: 'bug' }, 
    { name: 'Enhancement', value: 'enhancement' }
    ];

    $scope.scopeMessage = "default text";
    var changeCount = 0;

    $scope.onSelectChange = function()
    {
       changeCount++;
        this.message = "Change detected: " + changeCount;
    }.bind($scope);

    //$scope.form = {type : $scope.typeOptions[0].value};
    $scope.form = $scope.typeOptions[0].value;
}

And here is my HTML : 这是我的HTML

<div ng-controller="MyCtrl" class="row">
    <select ng-model='form' required ng-options='option.value as option.name for option in typeOptions' ng-change="onSelectChange"></select>

    <div style="border:1px solid black; width: 100px; height:20px;">{{scopeMessage}}<div>
</div>

This is currently holding me up on my project for work, so any help will be geatly appreciated. 这是目前我的工作项目,所以任何帮助将得到热烈的赞赏。 Thanks! 谢谢!

2 things... both simple :) 2件事......既简单:)

  1. ng-change=onSelectChange should be onSelectChange() ng-change=onSelectChange应该是onSelectChange()
  2. this.message should be this.scopeMessage this.message应该是this.scopeMessage

Your problem is that you are passing in the function reference to the ng-change directive. 您的问题是您正在将函数引用传递给ng-change指令。 However, that directive expects an expression which can evaluated. 但是,该指令需要一个可以计算的表达式。 So attach the parentheses to the function so that it can be evaluated as a function call. 因此,将括号附加到函数中,以便可以将其作为函数调用进行求值。

Like here: http://jsfiddle.net/MTfRD/1101/ 喜欢这里: http//jsfiddle.net/MTfRD/1101/

<select ng-model='form' required ng-options='option.value as option.name for option in typeOptions' ng-change="onSelectChange()"></select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM