I created a custom element directive which inserts an element inside the DOM and adds some javascript code for that element. My code looks like the following:
myDirective:
return {
restrict: 'E',
templateUrl: 'template.html',
scope: {
name: '@name'
},
controller: MyController,
controllerAs: 'vm',
bindToController: true
};
template.html:
<div id="{{vm.name}}" />
<script type="text/javascript">
var plugin = chart.generate({bindTo: '#{{vm.name}}'});
</script>
My call:
<my-directive name="test" />
The problem is that the injected javascript tries to generate a chart inside the div with id "test" but fails because at this stage angular hasn't yet inserted {{vm.name}} as the element id. How can I separate this javascript code so that it gets executed after angular inserts the id of the element? I looked up the post link method but am not sure if this is the right way to do.
//EDIT: To clarify: Basically I want to create a directive, which gets a parameter passed by an attribute (name) and uses this parameter to set it as the id of a div and use some javascript code to generate a chart inside that div.
var plugin = chart.generate({bindTo: '#{{vm.name}}'});in that directivespostLinkmethod.