没有调用AngularJS NgChange

时间:2014-05-07 09:46:40

标签: javascript jquery html angularjs

在Angularjs中,我添加了以下用于侦听更改事件的select控件

<select ng-model="Ruoli" ng-options="ruolo.Nome for ruolo in ruoli" class="form-control" ng-change="setNotRuolo(Ruoli.Id)"></select>

当我选择一项功能时

  

setNotRuolo()

被正确调用,但当我使用键盘上的键向下更改项目时,未调用该函数。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是一个已在 GitHub

中讨论过的问题

看看这个

<强> Working Demo

<强> HTML

<div ng-app='myApp' ng-controller="Controller">
    <label for="browsers">Pick A Name:</label>
    <form name="myForm">
        <select dselect required id="browsers" ng-model="selectValue" ng-options="browser for browser in browsers">
            <option value="">Select One</option>
        </select>
        <br>
        <button ng-click="submit()" ng-disabled="!myForm.$valid">submit</button>
    </form>
    <br>
    <br>You Selected: {{selectValue || "You didn't pick"}}</div>
</div>

<强>脚本

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

app.controller('Controller', function ($scope) {
    $scope.browsers = ['Manu', 'Mathew', 'Sunny'];
    $scope.submit = function () {
        alert("submit");
    }
});
if (navigator.userAgent.indexOf("Sunny") !== -1) {
    app.directive("select", function () {
        return {
            restrict: "E",
            require: "?ngModel",
            scope: false,
            link: function (scope, element, attrs, ngModel) {
                if (!ngModel) {
                    return;
                }
                element.bind("keyup", function () {
                    element.trigger("change");
                })
            }
        }
    })
}