Getter & setter support with ng-model in AngularJs

后端 未结 2 1351
遥遥无期
遥遥无期 2021-02-20 02:52

I am trying to get getter/setter support for ng-model by implementing a directive that will take care of getting and setting the values to/from the view/model. I am almost there

2条回答
  •  [愿得一人]
    2021-02-20 03:10

    NOTE AngularJs 1.3 now supports Getter/Setter for ng-model. Refer to http://docs.angularjs.org/api/ng/directive/ngModelOptions for more information.


    I could break the infinite loop with extra calls to

    ngModelCtrl.$setViewValue()
    

    and

    ngModelCtrl.$render()
    

    in the event handlers. Not sure if it's the best way to do it though.

    See fiddle: http://jsfiddle.net/BDyAs/12/

    EDIT:

    I improved the code even more in

    http://jsfiddle.net/BDyAs/15/

    by separating the directive in separate ones for the getter and the setter.

提交回复
热议问题