![](/img/trans.png)
[英]How to store a angular.js model variable in a javascript variable?
[英]Angular.js: How to store div HTML content in scope variable
我有一个可编辑的div
,我想将HTML内容存储在scope
变量scope.myText
:
<div id="editor" contenteditable="true" ng-model="myText">
<p>HTML Text</p>
</div>
我怎么能在Angular中做到这一点? 有没有解决这个问题的指令? 我感谢任何帮助,谢谢。
我使用ng-blur
指令找到了解决这个问题的方法:
控制器:
$scope.updateModel = function(){
$scope.myText = angular.element(editor).html();
//if you are using jquery use this line:
//$scope.myText = $('#editor').html();
};
如果要初始化编辑器内容,请使用以下命令:
angular.element(editor).html('<p>initial content</p>');
//thanks to @PatrickGrimard
或者使用jquery:
$('#editor').html('<p>initial content</p>');
视图:
<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.