So what I'm doing is adding a directive as an attribute to an element via a Factory. And I have a separate directive file that manipulates the DOM based on some event triggered on that element, the element being the one with newly added directive as an attribute. Now the thing that's happening is the directive file gets called first it searches the DOM doesn't find the directive tag. The factory called after directive successfully adds the attribute to DOM element but the problem is that attribute isn't recognised as a directive, it just assumes it to be a simple element attribute. Now what I want is the directive file to traverse the DOM again so that this time it can find the directive as an attribute tag and manipulate the DOM accordingly.
The naive version of the code is as follows:
index.html-
<html ng-app='myApp'>
<body ng-controller='myController'>
<div id="demo">
Clicking here should console Hola!!
<div>
//Script tags for all the 3 files above
</body>
</html>>
Controller File-
namespace angular {
var app = angular.module('myApp');
app.controller('myController', myController);
export class myController {
static $inject = ['myFactory'];
constructor(private myFactory: any) {
this.myFactory = myFactory;
console.log('myController Constructor Called');
}
}}
Factory File-
namespace angular {
var app = angular.module('myApp');
app.factory('myFactory', function () {
var node = document.getElementById("demo");;
node.setAttribute("directive-tag", "");
return "";
}); }
Directive File-
namespace std{
var app = angular.module('myApp', []);
app.directive('directive-tag', function () {
return {
restrict: 'E',
link: function (scope, element, attribute) {
console.log('Hola!');
}
};
}); }
PS : All this should happen on page load.
Any help would be appreciated!! Thanks in advance.