ngModel.$render is not called when model change

梦想与她 提交于 2020-01-02 08:04:08

问题


I'm trying to use tiny-mce with angular, but i have some trouble with the $render function in the directive. When i update the model, the $render function is not call.

Here a plunkr to illustrate: http://plnkr.co/edit/Ih1nDq?p=preview

I'm not sure, but i think it could be related to angular 1.2, because with angular 1.1.5, it works : http://plnkr.co/edit/LXAtHd?p=preview

Is this a bug of angular 1.2, or did i miss something new with angular 1.2?


回答1:


As far as I can see, the $render function is only called once. If you need to update your view on model changes you can add a function to the $viewChangeListeners Array:

ngModel.$viewChangeListeners.push(function () {
    updateView(ngModel.$viewValue);
});

I hope someone could give some more details why $render behaves different in Angular 1.2.




回答2:


This answer shows code you need in your directive to make render fire off when necessary:

TinyMCE <textarea> two way bound with AngularJS

        // When your model changes from the outside, use ngModel.$render to update the value in the textarea
        ngModel.$render = function () {
            textarea.val(ngModel.$viewValue);
        };

Also compare to this render function from angular-ui-tinymce ( https://github.com/angular-ui/ui-tinymce )

    ngModel.$render = function() {
      if (!tinyInstance) {
        tinyInstance = tinymce.get(attrs.id);
      }
      if (tinyInstance) {
        tinyInstance.setContent(ngModel.$viewValue || '');
      }

Plnkr: http://plnkr.co/edit/04AFkp?p=preview

However depending on the timing of the loading of your DOM you may need to set the priority on your directive upwards. :-)



来源:https://stackoverflow.com/questions/20244802/ngmodel-render-is-not-called-when-model-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!