I have a semi-complex directive. Here's the HTML which it uses as a template...
<div ng-hide="hidden" class="alert-container">
<div ng-class="startFade ? ['alert-box', 'fade', colorClass] : ['alert-box', colorClass]">
<span class="glyphicon glyphicon-alert"></span><p>{{ content }}</p>
</div>
</div>
And here's the relevant JS...
svs.directive("alertBox", function () {
return {
restrict: "E",
templateUrl: "resources/html/directives/alertBox.html",
link: function(scope, element, attrs, ctrl) {
ctrl.init(element);
},
controller: function($scope, $timeout, alertBoxService) {
var context = this;
function trigger(content, color) {
$scope.content = content;
$scope.colorClass = color;
$scope.hidden = false;
$scope.hide();
}
function init(element) {
context.element = element;
context.callback = trigger;
alertBoxService.register(context);
}
context.trigger = trigger;
context.init = init;
}
};
});
This directive compliments a service which I intend to inject into my controllers. Here's the service's JS...
svs.service("alertBoxService", function($compile) {
var alertBoxes = [];
var alertCounter = -1;
this.register = function(ctrl) {
alertBoxes[alertCounter] = ctrl;
}
this.trigger = function(content, color) {
alertCounter++;
angular.element(document.getElementById('alert-boxes')).append($compile("<alert-box></alert-box>"));
if (typeof(color) === 'undefined') color = "red";
alertBoxes[alertCounter].callback(content, color);
}
});
The idea is to have a service which I can inject into my controllers. When I call the trigger
method on that service, I can provide a color, and some content. The service then compiles a new instance of the directive, the newly compiled directive registers itself with the service, then the trigger
method passes the directive's callback a color and the content.
I believe my problem lies in compiling the new element. In the console, I get this error...
TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'
What is the issue here, and how can I modify my code to reach my desired result?