How to implement an ng-change for a custom directive

后端 未结 5 1052
滥情空心
滥情空心 2020-12-02 20:13

I have a directive with a template like

5条回答
  •  旧巷少年郎
    2020-12-02 20:25

    If you require ngModel you can just call $setViewValue on the ngModelController, which implicitly evaluates ng-change. The fourth parameter to the linking function should be the ngModelCtrl. The following code will make ng-change work for your directive.

    link : function(scope, element, attrs, ngModelCtrl){
        scope.updateModel = function(item) {
            ngModelCtrl.$setViewValue(item);
        }
    }
    

    In order for your solution to work, please remove ngChange and ngModel from isolate scope of myDirective.

    Here's a plunk: http://plnkr.co/edit/UefUzOo88MwOMkpgeX07?p=preview

提交回复
热议问题