Service portal, ng-include, Angular ng-templates

scottl
Kilo Sage

Does anyone know how to compile these local widget 'ng-templates' through the controller, instead of through the view?

View - works

<div ng-include="'_xxxtemplatexxx_'"></div>

Outputs: "template"

Controller - Doesn't work

var compiled = $compile("<div ng-include=\"'_xxxtemplatexxx_'\"></div>")($scope);

console.log(compiled[0]);

Outputs: object with no "nodes" i.e empty.

When using $complie the basic template content isn't 'compiled' even if plain text is within it.   Any ideas?

1 ACCEPTED SOLUTION

Thanks Siva, I managed to figure it out.   Wrapping it with another element [div] was key in getting it to work also.



var compiled = $compile("<div><ng-include src=\"'_xxxtemplatexxx_'\"></ng-include></div>")($scope);




View solution in original post

2 REPLIES 2

siva_dirisala
ServiceNow Employee
ServiceNow Employee

Hello Scott, I haven't tried but what you are trying is not even a common practice in AngularJS let alone Service Portal. See this



javascript - how can we use $compile outside a directive in Angularjs - Stack Overflow and see if that helps.


Thanks Siva, I managed to figure it out.   Wrapping it with another element [div] was key in getting it to work also.



var compiled = $compile("<div><ng-include src=\"'_xxxtemplatexxx_'\"></ng-include></div>")($scope);