I want to create a new element within a service, compile it and append it to the body (a message box). My question is if and how it's possible to create a new element in a service and append it to the DOM and if there's a better way to do it maybe (it should be in a service tho).
My problem is that I can't bind attributes to the directive's scope:
I got the following code:
Method to create new element in my service
_createMessageBox = function (text, buttons) {
var msgBox = document.createElement('message-box');
msgBox.setAttribute('class', 'messageBox');
msgBox.setAttribute('text', text);
msgBox.setAttribute('buttons', buttons);
$compile(msgBox)(scope);
$('body').append(msgBox);
};
Use of the service
var buttons = ['Eins', 'Zwei'];
mbs.createMessageBox('Nachricht', buttons);
directive
app.directive('messageBox', function () {
link = function (scope, element, attrs) {
scope.buttonClicked = function(id) {
this.$emit('button'+id+'_clicked');
}
}
return {
restrict: 'E',
scope:
{
text: '=',
buttons: '='
},
link: link
};
});
When I do it that way I get this error and the attributes aren't binded
Error: [$parse:syntax] Syntax Error: Token ',' is an unexpected token at column 5 of the expression [Eins,Zwei] starting at [,Zwei].