javascript 在 angular 指令中获取原始嵌入内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20102059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get original transcluded content within angular directive
提问by prototype
My goal is to create an editable
directive that allows a user to edit HTML of any element to which the attribute is attached (see Plunker: http://plnkr.co/edit/nIrr9Lu0PZN2PdnhQOC6)
我的目标是创建一个editable
指令,允许用户编辑属性附加到的任何元素的 HTML(请参阅 Plunker:http://plnkr.co/edit/nIrr9Lu0PZN2PdnhQOC6 )
This almostworks except I can't get the original raw HTML of the transcluded content to initialize the text area. I can get the text of it from clone.text()
, but that's missing the HTML tags like <H1>
, <div>
, etc. so clicking apply with no edits is not idempotent.
这几乎可以工作,除非我无法获得嵌入内容的原始原始 HTML 来初始化文本区域。我可以得到它从文clone.text()
,但缺少像HTML标签<H1>
,<div>
等等,以便点击没有修改申请不幂等。
The method clone.html()
throws an error, Cannot read property 'childNodes' of undefined
该方法clone.html()
抛出错误,Cannot read property 'childNodes' of undefined
app.directive("editable", function($rootScope) {
return {
restrict: "A",
templateUrl: "mytemplate.html",
transclude: true,
scope: {
content: "=editContent"
},
controller: function($scope, $element, $compile, $transclude, $sce) {
// Initialize the text area with the original transcluded HTML...
$transclude(function(clone, scope) {
// This almost works but strips out tags like <h1>, <div>, etc.
// $scope.editContent = clone.text().trim();
// this works much better per @Emmentaler, tho contains expanded HTML
var html = "";
for (var i=0; i<clone.length; i++) {
html += clone[i].outerHTML||'';}
});
$scope.editContent = html;
$scope.onEdit = function() {
// HACK? Using jQuery to place compiled content
$(".editable-output",$element).html(
// compiling is necessary to render nested directives
$compile($scope.editContent)($rootScope)
);
}
$scope.showEditor = false;
$scope.toggleEditor = function() {
$scope.showEditor = !$scope.showEditor;
}
}
}
});
(This question is essentially a wholesale rewrite of the question and code after an earlier attempt to frame the question, Get original transcluded content in Angular directive)
(这个问题本质上是在较早尝试框架问题之后对问题和代码的大规模重写,在 Angular 指令中获取原始嵌入内容)
回答by Nathaniel Johnson
The $element.innerHTML
should contain the original HTML. I am showing that it contains
本$element.innerHTML
应包含原始的HTML。我显示它包含
<div class="editable">
<span class="glyphicon glyphicon-edit" ng-click="toggleEditor()"></span>
<div class="editable-input" ng-show="showEditor">
<b><p>Enter well-formed HTML content:</p></b>
<p>E.g.<code><h1>Hello</h1><p>some text</p><clock></clock></code></p>
<textarea ng-model="editContent"></textarea>
<button class="btn btn-primary" ng-click="onEdit()">apply</button>
</div>
<div class="editable-output" ng-transclude=""></div>
</div>