javascript - Angular ng-change 不适用于指令的模板

标签 javascript angularjs angularjs-ng-change

我正在尝试使用 Angular 指令制作一个提示性的 ajax 搜索框。我仍在开始匹配数据,这是我所拥有的:

function hintSearch () {
    return {
        restrict: 'E',
        replace: true,
        template: '<div class="search_hint"><label><input type="search" ng-change="query()"></label><ul class="results"><li class="hint" ng-repeat="hint in hints | limitTo: 8" ng-bind="hint" ng-click="hint_selected(hint)"></li></ul></div>',
        scope: {},
        link: function(scope, element, attrs){
            scope.hints = ["client1", "client2"];
            scope.hint_selected = function(){
                console.log("hint selected");
            }
            scope.query = function(){
                console.log("query php");
            scope.hints = ["client1", "client2", "client3"];
            }
        }
    }
}

问题是 ng-change 给我一个错误。使用 ng-clickng-keypress 它可以完美工作,所以没有任何意义!有什么想法吗?

这是它抛出的错误:

angular.js:13550 Error: [$compile:ctreq] http://errors.angularjs.org/1.5.5/$compile/ctreq?p0=ngModel&p1=ngChange
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:6:412
    at gb (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:71:251)
    at n (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:66:67)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:58:305)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:58:322)
    at n (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:65:473)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:58:305)
    at n (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:65:473)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js:58:305)

最佳答案

From error page :

This error occurs when HTML compiler tries to process a directive that specifies the require option in a directive definition, but the required directive controller is not present on the current DOM element (or its ancestor element, if ^ was specified).

This is the source code of ng-change .

var ngChangeDirective = valueFn({
  restrict: 'A',
  require: 'ngModel',
  link: function(scope, element, attr, ctrl) {
    ctrl.$viewChangeListeners.push(function() {
      scope.$eval(attr.ngChange);
    });
  }
});

ng-modelng-change 所必需的,您的input 中没有ng-model .

<input type="search" ng-change="query()">

ng-model 添加到您的 input 中,希望能解决您的问题。

<input type="search" ng-model='myModel' ng-change="query()">

关于javascript - Angular ng-change 不适用于指令的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36742130/

相关文章:

javascript - 相当于 div 的 window.scrollY

javascript - Yii2:如何使用Ajax获取数组对象?

javascript - 两个 AngularJS 服务之间的消息传递

javascript - ngRepeat 参数到 ngChange 函数

javascript - ng-change 不调用函数

javascript - 如何访问对象字面量中的内容?

javascript - AOL 样式问题 - 两个固定的侧边栏,一个流畅的内容布局

javascript - 如何删除 AngularJS 中的动态 ng-hide 类?

Javascript - 如何清除最后绘制的矩形,以便在 'dragging' 创建矩形时仅绘制 1

javascript - 何时在 Angularjs 中使用 $watch 或 ng-change