admin管理员组文章数量:1293739
Is it possible to programmatically get the original transcluded content within an Angular.js directive?
I'm trying to create an an editable
directive which can be added to any div, allowing the user to edit the HTML content with custom angular directives. (The design goal is to avoid the need to add infinite configuration GUI features in the app, as power users can just edit the HTML...), e.g.:
<div editable>
<h1>Lorem Ipsem</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
<clock>A custom directive</clock>
</div>
See this Plunker as an example ():
- Click the edit icon on the solid grey bar to open the editor
- Enter in any well formed HTML with tags:
(e.g.
<h1>A title</h1><p>some content</p><clock></clock>
) - Click "Apply"
What I like about this so far:
- It can be added to any div
- It can include nested custom directives, by using $pile
- It includes the transcluded content
What I can't figure out yet:
- How to get the raw transcluded content to initialize the textarea
Within the pile function, $transclude
seems to contain the template for mydirective
, and with the controller function, $transclude
contains the post-piled content after things have been changed, directives rendered, etc.
Is it possible to programmatically get the original transcluded content within an Angular.js directive?
I'm trying to create an an editable
directive which can be added to any div, allowing the user to edit the HTML content with custom angular directives. (The design goal is to avoid the need to add infinite configuration GUI features in the app, as power users can just edit the HTML...), e.g.:
<div editable>
<h1>Lorem Ipsem</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
<clock>A custom directive</clock>
</div>
See this Plunker as an example (http://plnkr.co/edit/nIrr9Lu0PZN2PdnhQOC6?p=preview):
- Click the edit icon on the solid grey bar to open the editor
- Enter in any well formed HTML with tags:
(e.g.
<h1>A title</h1><p>some content</p><clock></clock>
) - Click "Apply"
What I like about this so far:
- It can be added to any div
- It can include nested custom directives, by using $pile
- It includes the transcluded content
What I can't figure out yet:
- How to get the raw transcluded content to initialize the textarea
Within the pile function, $transclude
seems to contain the template for mydirective
, and with the controller function, $transclude
contains the post-piled content after things have been changed, directives rendered, etc.
-
2
isn't the content ing from scope? If so pass that as attribute into nested directives. A demo would help. Or do your own
$pile
on first directive...hard to answer without seeing more directive code – charlietfl Commented Nov 17, 2013 at 15:38 - As @charlietfl suggested, a demo of what you already have, will give us more info to help – dimirc Commented Nov 17, 2013 at 16:04
- @charlietfl -- You're absolutely right, created a Plunker. The short question now is how to pre-populate the text area with the original (raw, prepiled) transcluded content, so the user is editing it, not retyping it. – prototype Commented Nov 18, 2013 at 3:53
-
1
still looking at it wrong...if data is in scope variable...use
ng-model
to bind to a textarea, input, select etc – charlietfl Commented Nov 18, 2013 at 4:02 - @charlietfl -- Very helpful. I've rewritten the plunker and question, narrowing it down to a specific issue about getting the html vs the text of the transcluded content. stackoverflow./questions/20102059/… – prototype Commented Nov 20, 2013 at 16:58
1 Answer
Reset to default 6You can use transclude function:
.directive('editable', function() {
return {
transclude: true,
link: function(scope, element, attrs, ctrl, transclude) {
transclude(scope, function(clone) {
// clone is your transluded content
});
}
};
});
本文标签: javascriptGet original transcluded content in Angular directiveStack Overflow
版权声明:本文标题:javascript - Get original transcluded content in Angular directive - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741589096a2387016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论