Why is ngModel.$setViewValue(…) not working from

后端 未结 3 1113
清歌不尽
清歌不尽 2020-12-08 02:50

I\'m writing an directive which needs an isolated scope, but I want to bind it to the parent scope via ngModel.

Here the problem is that the parent\'s scope value is

3条回答
  •  攒了一身酷
    2020-12-08 03:25

    the first answer explains the problem well, I believe i have a simpler solution that avoids extra watches.

    to summarize answer 1. ngModel can not work inside the isolate scope because the elements you intended it to bind to are not in its scope. they are in the parent scope.

    solution 1, bind to the parent's property

    Change me!

    becomes

    Change me!

    solution 2, move ngModel outside the isolate scope

    require : '?ngModel', becomes require : '?^ngModel', the ^ tells your directive to look in parent elements for ngModel

    Change me!

    becomes

    Change me!

提交回复
热议问题