I am writing a rather complicated application in Angularjs. This is big enough to embarrass me. I am exploring Angular deeper and I can see that my code is bad. I understand this concept:
module.directive('createControl', function($compile, $timeout){ scope: { // scope bindings with '=' & '@' }, template: '<div>Template string with binded {{ variables }}</div>', link: function(scope, element, attrs){ // Function with logic. Should watch scope. }
I have a few problems:
- My template is complex, I have part of a template that works dynamically in the link function
- I need to add a compiled template to the element, not replace it.
- With the concept above, my template is added without any interpolation ...
So, my code looks in a simplified form:
module.directive('createControl', function($compile, $timeout){ scope: { var1: '@var1', var2: '@var2', var3: '@var3' }, template: '<div>{{ var1 }} {{ var3 }}</div>', link: function(scope, element, attrs){ $('.someelement').on('event', function(){ var2 = 'SNIPPET';
My questions:
How to compile my template with scope variables?
How to view scope variables?
Should I split my directive in two? If I have to, how to do it right?
I159
source share