How to implement an ng-change for a custom directive

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

I have a directive with a template like

5条回答
  •  悲&欢浪女
    2020-12-02 20:24

    The fundamental issue here is that the underlying model does not get updated until the digest cycle that happens after scope.updateModel has finished executing. If the ngChange function requires details of the update that is being made then those details can be made available explicitly to ngChange, rather than relying on the model updating having been previously applied.

    This can be done by providing a map of local variable names to values when calling ngChange. In this scenario, you can mapping the new value of the model to a name which can be referenced in the ng-change expression.

    For example:

    scope.updateModel = function(item)
    {
        scope.ngModel = item;
        scope.ngChange({newValue: item});
    }
    

    In the HTML:

    
    

    See: http://plnkr.co/edit/4CQBEV1S2wFFwKWbWec3?p=preview

提交回复
热议问题