模型更改时不调用 ngModel.$render [英] ngModel.$render is not called when model change
问题描述
我正在尝试将 tiny-mce 与 angular 结合使用,但是指令中的 $render 函数遇到了一些问题.当我更新模型时,不会调用 $render 函数.
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.
这里有一个 plunkr 来说明:http://plnkr.co/edit/Ih1nDq?p=预览
Here a plunkr to illustrate: http://plnkr.co/edit/Ih1nDq?p=preview
我不确定,但我认为这可能与 angular 1.2 有关,因为对于 angular 1.1.5,有用 :http://plnkr.co/edit/LXAtHd?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
这是 angular 1.2 的错误,还是我错过了 angular 1.2 的一些新东西?
Is this a bug of angular 1.2, or did i miss something new with angular 1.2?
推荐答案
这个答案显示了您在指令中需要的代码,以便在必要时关闭渲染:
This answer shows code you need in your directive to make render fire off when necessary:
// When your model changes from the outside, use ngModel.$render to update the value in the textarea
ngModel.$render = function () {
textarea.val(ngModel.$viewValue);
};
还要与 angular-ui-tinymce 的这个渲染函数进行比较(https://github.com/angular-ui/ui-tinymce )
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一个>
然而,根据加载 DOM 的时间,您可能需要向上设置指令的优先级.:-)
However depending on the timing of the loading of your DOM you may need to set the priority on your directive upwards. :-)
这篇关于模型更改时不调用 ngModel.$render的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!