[英]Angular.js: How to store div HTML content in scope variable
I have an editable div
and I want to store the HTML content in scope
variable scope.myText
: 我有一个可编辑的div
,我想将HTML内容存储在scope
变量scope.myText
:
<div id="editor" contenteditable="true" ng-model="myText">
<p>HTML Text</p>
</div>
How can I do that in Angular? 我怎么能在Angular中做到这一点? Is there any directive to solve this problem? 有没有解决这个问题的指令? I appreciate any help, thanks. 我感谢任何帮助,谢谢。
I found the solution to this problem using ng-blur
directive: 我使用ng-blur
指令找到了解决这个问题的方法:
Controller: 控制器:
$scope.updateModel = function(){
$scope.myText = angular.element(editor).html();
//if you are using jquery use this line:
//$scope.myText = $('#editor').html();
};
If you want to initialize the editor content use this: 如果要初始化编辑器内容,请使用以下命令:
angular.element(editor).html('<p>initial content</p>');
//thanks to @PatrickGrimard
or with jquery: 或者使用jquery:
$('#editor').html('<p>initial content</p>');
View: 视图:
<div id="editor" contenteditable="true" data-ng-blur="updateModel()">
<p>HTML Text</p>
</div>
{{myText}}
为什么不在div中使用textarea,那么你可以在它上面使用ng-model。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.