【问题标题】:Angular.js: How to store div HTML content in scope variableAngular.js:如何将 div HTML 内容存储在范围变量中
【发布时间】:2014-09-28 18:16:49
【问题描述】:

我有一个可编辑的 div,我想将 HTML 内容存储在 scope 变量 scope.myText 中:

<div id="editor" contenteditable="true" ng-model="myText">
    <p>HTML Text</p>
</div>

如何在 Angular 中做到这一点?有没有解决这个问题的指令?感谢您的帮助,谢谢。

【问题讨论】:

  • 所见即所得叫什么?因为也有 tinymce github.com/angular-ui/ui-tinymce 和 ckeditor 的角度插件,但不确定它有多完整。如果您只需要所见即所得的浅色文本,甚至更好 - textangular.com
  • 您看到的 html 控件,如文本、输入等,实际上都是指令,因此 ng-model 可以工作。 div 没有指令。
  • 嗨@Dylan,我正在使用SnapEditor,我已经使用适合我的ng-blur 进行了实现。感谢您的建议,我将看看 textangular。
  • 你考虑过这个吗? textangular.com
  • 是的,但我需要一个带有浮动工具栏的编辑器,因此我使用的是 SnapEditor,谢谢

标签: javascript html angularjs angularjs-directive angularjs-scope


【解决方案1】:

我使用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}}

【讨论】:

    【解决方案2】:

    为什么不在你的 div 中使用 textarea,然后你可以在它上面使用 ng-model。

    【讨论】:

    • 不,我不能使用 textarea,因为 div 是 WYSIWYG 编辑器,它不使用 textarea。
    • 您可以使用 angular.element() 来获取您的 div,然后调用 .html() 来获取内容并将其存储在范围内。
    • 谢谢,但是如何使用 angular.select 来做到这一点?在文档中 angular.select 指的是 HTML 组件 .
    • 我不确定你指的是什么。我正在为解决方案提出建议。
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 2016-10-15
    • 2011-03-26
    相关资源
    最近更新 更多