I have a function in my AngularJS controller:
//some stuff from controller
var variable1;
var variable2;
$scope.setTitle = function () {
if ( //sth) {
variable1 = "string"
return variable1;
}
else {
(function () {
//setting the variable2 - it will be the HTML code
variable2 = angular.element(document.querySelector('titleID'));
variable2.append(title);
})();
return variable2;
}
};
I'm getting the title from JSON file and it looks like:
title = "Return product: <span data-ng-bind=\"productRet.ID\" />"
I have to use the JSON file, because I have a big "tree" inside this JSON file, and the title is different according to what will happen inside the function in else statement
I'm calling setTitle() in my template in directive:
.directive('myDirective', ['$compile', function ($compile) {
return {
restrict: 'E',
link: function (scope, element, attrs) {
var template = {
<!-- a lot of HTML -->
+ "<div id=\"titleID\">"
+ "<span class=\"navbar-brand\" > {{setTitle()}} </span>"
+ </div>
};
templateObj = $compile(template)(scope);
element.append(templateObj);
}
}
}]);
If the result from setTitle() is the variable1 everything is fine. The problem is when the result is variable2 because I'm getting an error:
"[$interpolate:interr] Can't interpolate: {{setTitle()}}
Error: [$parse:isecdom] Referencing DOM nodes in Angular expressions is disallowed! Expression: setTitle()
How to properly insert the HTML code from variable2 into my template??
ng-show,ng-click, etc.