ng-change不适用于ng-select

Web*_*rer 13 javascript angularjs

我正在使用填充的选择框ng-options.不幸的是,我无法ng-change调用我的函数.

这是我的小提琴

这是我的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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是目前我的工作项目,所以任何帮助将得到热烈的赞赏.谢谢!

Tim*_*thy 16

2件事......既简单:)

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

  • 对不起,我整整一年没有复习这个.是的@MichaelMahony你是对的.是时候解决问题. (2认同)

Mik*_*aas 6

您的问题是您正在将函数引用传递给ng-change指令.但是,该指令需要一个可以计算的表达式.因此,将括号附加到函数中,以便可以将其作为函数调用进行求值.

喜欢这里: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>
Run Code Online (Sandbox Code Playgroud)