Angular 4: Custom directive content not rendering for innerHTML
I was trying to create dynamic tree view component in angular 4. I have an array which have the data to form the dynamic tree view.
I tried to create a custom directive to achieve this. Here is my code :
import { Directive,ElementRef, Input, HostListener } from '@angular/core';
@Directive({
selector: '[appTreeView]'
})
export class TreeViewDirective {
@Input('appTreeView') categories:any;
@Input() config:any = {
categoryList: null,
children : ,
nodeLabel : '',
treeId : '1',
nodeId : '12'
};
elRef: any;
constructor(el: ElementRef) {
console.log(Element);
console.log(this.config);
console.log(this.categories);
this.elRef = el;
}
ngOnInit(){
console.log('input : ',this.categories);
this.elRef.nativeElement.innerHTML = '<ul>' +
'<li *ngFor="let node in ' + this.categories + '; let indx = index">' +
'<i class="collapsed" data-ng-show="node.' + this.config.children + '.length && node.collapsed" data-ng-click="' + this.config.treeId + '.selectNodeHead(node)"></i>' +
'<i class="expanded" data-ng-show="node.' + this.config.children + '.length && !node.collapsed" data-ng-click="' + this.config.treeId + '.selectNodeHead(node)"></i>' +
'<i class="normal" data-ng-hide="node.' + this.config.children + '.length"></i> ' +
'<span data-ng-class="node.selected" data-ng-click="selectNodeLabel(node)">{{node.' + this.config.nodeLabel + '}}</span>' +
'<div data-ng-hide="node.collapsed" data-tree-id="' + this.config.treeId + '" data-tree-model="node.' + this.config.children + '" data-node-id=' + this.config.nodeId + ' data-node-label=' + this.config.nodeLabel + ' data-node-children=' + this.config.children + '></div>' +
'</li>' +
'</ul>';
}
}
In my component html :
<div [appTreeView]="categoryListArray"></div>
In component ts :
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-tree-view-test',
templateUrl: './tree-view-test.component.html',
styleUrls: ['./tree-view-test.component.css']
})
export class TreeViewTestComponent implements OnInit {
categoryListArray:any = ;
constructor() { }
ngOnInit() {
this.categoryListArray = [
{ "label" : "User", "id" : "role1", "children" : [
{ "label" : "subUser1", "id" : "role11", "children" : },
{ "label" : "subUser2", "id" : "role12", "children" : [
{ "label" : "subUser2-1", "id" : "role121", "children" : [
{ "label" : "subUser2-1-1", "id" : "role1211", "children" : },
{ "label" : "subUser2-1-2", "id" : "role1212", "children" : }
]}
]}
]},
{ "label" : "Admin", "id" : "role2", "children" : },
{ "label" : "Guest", "id" : "role3", "children" : }
];
}
onCategoryClick(event){
console.log('clicked item : ', event);
}
}
Here is the output how am getting :

Please check the inspect view, my code is not rendered properly. *ngFor is still there and I am able to see interpolation symbol in my output. Don't know why my custom directive is not working. Here is the console output for reference.

add a comment |
I was trying to create dynamic tree view component in angular 4. I have an array which have the data to form the dynamic tree view.
I tried to create a custom directive to achieve this. Here is my code :
import { Directive,ElementRef, Input, HostListener } from '@angular/core';
@Directive({
selector: '[appTreeView]'
})
export class TreeViewDirective {
@Input('appTreeView') categories:any;
@Input() config:any = {
categoryList: null,
children : ,
nodeLabel : '',
treeId : '1',
nodeId : '12'
};
elRef: any;
constructor(el: ElementRef) {
console.log(Element);
console.log(this.config);
console.log(this.categories);
this.elRef = el;
}
ngOnInit(){
console.log('input : ',this.categories);
this.elRef.nativeElement.innerHTML = '<ul>' +
'<li *ngFor="let node in ' + this.categories + '; let indx = index">' +
'<i class="collapsed" data-ng-show="node.' + this.config.children + '.length && node.collapsed" data-ng-click="' + this.config.treeId + '.selectNodeHead(node)"></i>' +
'<i class="expanded" data-ng-show="node.' + this.config.children + '.length && !node.collapsed" data-ng-click="' + this.config.treeId + '.selectNodeHead(node)"></i>' +
'<i class="normal" data-ng-hide="node.' + this.config.children + '.length"></i> ' +
'<span data-ng-class="node.selected" data-ng-click="selectNodeLabel(node)">{{node.' + this.config.nodeLabel + '}}</span>' +
'<div data-ng-hide="node.collapsed" data-tree-id="' + this.config.treeId + '" data-tree-model="node.' + this.config.children + '" data-node-id=' + this.config.nodeId + ' data-node-label=' + this.config.nodeLabel + ' data-node-children=' + this.config.children + '></div>' +
'</li>' +
'</ul>';
}
}
In my component html :
<div [appTreeView]="categoryListArray"></div>
In component ts :
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-tree-view-test',
templateUrl: './tree-view-test.component.html',
styleUrls: ['./tree-view-test.component.css']
})
export class TreeViewTestComponent implements OnInit {
categoryListArray:any = ;
constructor() { }
ngOnInit() {
this.categoryListArray = [
{ "label" : "User", "id" : "role1", "children" : [
{ "label" : "subUser1", "id" : "role11", "children" : },
{ "label" : "subUser2", "id" : "role12", "children" : [
{ "label" : "subUser2-1", "id" : "role121", "children" : [
{ "label" : "subUser2-1-1", "id" : "role1211", "children" : },
{ "label" : "subUser2-1-2", "id" : "role1212", "children" : }
]}
]}
]},
{ "label" : "Admin", "id" : "role2", "children" : },
{ "label" : "Guest", "id" : "role3", "children" : }
];
}
onCategoryClick(event){
console.log('clicked item : ', event);
}
}
Here is the output how am getting :

Please check the inspect view, my code is not rendered properly. *ngFor is still there and I am able to see interpolation symbol in my output. Don't know why my custom directive is not working. Here is the console output for reference.

you should use structural directives for manipulating DOM
– Artyom Amiryan
Nov 15 '18 at 13:56
1
By doingthis.elRef.nativeElement.innerHTML = "..."you add content directly to the DOM outside of an angular context (ngZone). So none of the angular components or directives will work here. Components are more suited for what you are trying to accomplish. I recommend you to learn the difference between components and directives. Or maybe check out angular ui libraries to find how they implemented tree view components.
– Lars-Kristian Johansen
Nov 15 '18 at 14:05
add a comment |
I was trying to create dynamic tree view component in angular 4. I have an array which have the data to form the dynamic tree view.
I tried to create a custom directive to achieve this. Here is my code :
import { Directive,ElementRef, Input, HostListener } from '@angular/core';
@Directive({
selector: '[appTreeView]'
})
export class TreeViewDirective {
@Input('appTreeView') categories:any;
@Input() config:any = {
categoryList: null,
children : ,
nodeLabel : '',
treeId : '1',
nodeId : '12'
};
elRef: any;
constructor(el: ElementRef) {
console.log(Element);
console.log(this.config);
console.log(this.categories);
this.elRef = el;
}
ngOnInit(){
console.log('input : ',this.categories);
this.elRef.nativeElement.innerHTML = '<ul>' +
'<li *ngFor="let node in ' + this.categories + '; let indx = index">' +
'<i class="collapsed" data-ng-show="node.' + this.config.children + '.length && node.collapsed" data-ng-click="' + this.config.treeId + '.selectNodeHead(node)"></i>' +
'<i class="expanded" data-ng-show="node.' + this.config.children + '.length && !node.collapsed" data-ng-click="' + this.config.treeId + '.selectNodeHead(node)"></i>' +
'<i class="normal" data-ng-hide="node.' + this.config.children + '.length"></i> ' +
'<span data-ng-class="node.selected" data-ng-click="selectNodeLabel(node)">{{node.' + this.config.nodeLabel + '}}</span>' +
'<div data-ng-hide="node.collapsed" data-tree-id="' + this.config.treeId + '" data-tree-model="node.' + this.config.children + '" data-node-id=' + this.config.nodeId + ' data-node-label=' + this.config.nodeLabel + ' data-node-children=' + this.config.children + '></div>' +
'</li>' +
'</ul>';
}
}
In my component html :
<div [appTreeView]="categoryListArray"></div>
In component ts :
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-tree-view-test',
templateUrl: './tree-view-test.component.html',
styleUrls: ['./tree-view-test.component.css']
})
export class TreeViewTestComponent implements OnInit {
categoryListArray:any = ;
constructor() { }
ngOnInit() {
this.categoryListArray = [
{ "label" : "User", "id" : "role1", "children" : [
{ "label" : "subUser1", "id" : "role11", "children" : },
{ "label" : "subUser2", "id" : "role12", "children" : [
{ "label" : "subUser2-1", "id" : "role121", "children" : [
{ "label" : "subUser2-1-1", "id" : "role1211", "children" : },
{ "label" : "subUser2-1-2", "id" : "role1212", "children" : }
]}
]}
]},
{ "label" : "Admin", "id" : "role2", "children" : },
{ "label" : "Guest", "id" : "role3", "children" : }
];
}
onCategoryClick(event){
console.log('clicked item : ', event);
}
}
Here is the output how am getting :

Please check the inspect view, my code is not rendered properly. *ngFor is still there and I am able to see interpolation symbol in my output. Don't know why my custom directive is not working. Here is the console output for reference.

I was trying to create dynamic tree view component in angular 4. I have an array which have the data to form the dynamic tree view.
I tried to create a custom directive to achieve this. Here is my code :
import { Directive,ElementRef, Input, HostListener } from '@angular/core';
@Directive({
selector: '[appTreeView]'
})
export class TreeViewDirective {
@Input('appTreeView') categories:any;
@Input() config:any = {
categoryList: null,
children : ,
nodeLabel : '',
treeId : '1',
nodeId : '12'
};
elRef: any;
constructor(el: ElementRef) {
console.log(Element);
console.log(this.config);
console.log(this.categories);
this.elRef = el;
}
ngOnInit(){
console.log('input : ',this.categories);
this.elRef.nativeElement.innerHTML = '<ul>' +
'<li *ngFor="let node in ' + this.categories + '; let indx = index">' +
'<i class="collapsed" data-ng-show="node.' + this.config.children + '.length && node.collapsed" data-ng-click="' + this.config.treeId + '.selectNodeHead(node)"></i>' +
'<i class="expanded" data-ng-show="node.' + this.config.children + '.length && !node.collapsed" data-ng-click="' + this.config.treeId + '.selectNodeHead(node)"></i>' +
'<i class="normal" data-ng-hide="node.' + this.config.children + '.length"></i> ' +
'<span data-ng-class="node.selected" data-ng-click="selectNodeLabel(node)">{{node.' + this.config.nodeLabel + '}}</span>' +
'<div data-ng-hide="node.collapsed" data-tree-id="' + this.config.treeId + '" data-tree-model="node.' + this.config.children + '" data-node-id=' + this.config.nodeId + ' data-node-label=' + this.config.nodeLabel + ' data-node-children=' + this.config.children + '></div>' +
'</li>' +
'</ul>';
}
}
In my component html :
<div [appTreeView]="categoryListArray"></div>
In component ts :
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-tree-view-test',
templateUrl: './tree-view-test.component.html',
styleUrls: ['./tree-view-test.component.css']
})
export class TreeViewTestComponent implements OnInit {
categoryListArray:any = ;
constructor() { }
ngOnInit() {
this.categoryListArray = [
{ "label" : "User", "id" : "role1", "children" : [
{ "label" : "subUser1", "id" : "role11", "children" : },
{ "label" : "subUser2", "id" : "role12", "children" : [
{ "label" : "subUser2-1", "id" : "role121", "children" : [
{ "label" : "subUser2-1-1", "id" : "role1211", "children" : },
{ "label" : "subUser2-1-2", "id" : "role1212", "children" : }
]}
]}
]},
{ "label" : "Admin", "id" : "role2", "children" : },
{ "label" : "Guest", "id" : "role3", "children" : }
];
}
onCategoryClick(event){
console.log('clicked item : ', event);
}
}
Here is the output how am getting :

Please check the inspect view, my code is not rendered properly. *ngFor is still there and I am able to see interpolation symbol in my output. Don't know why my custom directive is not working. Here is the console output for reference.

asked Nov 15 '18 at 13:09
SANGEETH KUMAR S GSANGEETH KUMAR S G
181220
181220
you should use structural directives for manipulating DOM
– Artyom Amiryan
Nov 15 '18 at 13:56
1
By doingthis.elRef.nativeElement.innerHTML = "..."you add content directly to the DOM outside of an angular context (ngZone). So none of the angular components or directives will work here. Components are more suited for what you are trying to accomplish. I recommend you to learn the difference between components and directives. Or maybe check out angular ui libraries to find how they implemented tree view components.
– Lars-Kristian Johansen
Nov 15 '18 at 14:05
add a comment |
you should use structural directives for manipulating DOM
– Artyom Amiryan
Nov 15 '18 at 13:56
1
By doingthis.elRef.nativeElement.innerHTML = "..."you add content directly to the DOM outside of an angular context (ngZone). So none of the angular components or directives will work here. Components are more suited for what you are trying to accomplish. I recommend you to learn the difference between components and directives. Or maybe check out angular ui libraries to find how they implemented tree view components.
– Lars-Kristian Johansen
Nov 15 '18 at 14:05
you should use structural directives for manipulating DOM
– Artyom Amiryan
Nov 15 '18 at 13:56
you should use structural directives for manipulating DOM
– Artyom Amiryan
Nov 15 '18 at 13:56
1
1
By doing
this.elRef.nativeElement.innerHTML = "..." you add content directly to the DOM outside of an angular context (ngZone). So none of the angular components or directives will work here. Components are more suited for what you are trying to accomplish. I recommend you to learn the difference between components and directives. Or maybe check out angular ui libraries to find how they implemented tree view components.– Lars-Kristian Johansen
Nov 15 '18 at 14:05
By doing
this.elRef.nativeElement.innerHTML = "..." you add content directly to the DOM outside of an angular context (ngZone). So none of the angular components or directives will work here. Components are more suited for what you are trying to accomplish. I recommend you to learn the difference between components and directives. Or maybe check out angular ui libraries to find how they implemented tree view components.– Lars-Kristian Johansen
Nov 15 '18 at 14:05
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320219%2fangular-4-custom-directive-content-not-rendering-for-innerhtml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320219%2fangular-4-custom-directive-content-not-rendering-for-innerhtml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you should use structural directives for manipulating DOM
– Artyom Amiryan
Nov 15 '18 at 13:56
1
By doing
this.elRef.nativeElement.innerHTML = "..."you add content directly to the DOM outside of an angular context (ngZone). So none of the angular components or directives will work here. Components are more suited for what you are trying to accomplish. I recommend you to learn the difference between components and directives. Or maybe check out angular ui libraries to find how they implemented tree view components.– Lars-Kristian Johansen
Nov 15 '18 at 14:05