If you want to not push anything onto the stack, you can use the link function, where you can access attributes through attrs . Here are two approaches to this:
Communication function 1 function:
Instead of using a template, you should use this link function in your directive, which conditionally adds your template:
link: function (scope, element, attrs) { if (attrs.callbackExpression) { var html = '<button>Fire callback</button>'; element.replaceWith(html); } }
Option 1: http://jsfiddle.net/ZC4MZ/2/
Link function 2 function (better for large templates):
For large templates, you can use $templateCache . First you add a template:
myApp.run(function($templateCache) { $templateCache.put('myDirective.html', '<button>Fire callback</button>'); });
Then use it conditionally, as option 1, but with $templateCache.get() :
link: function (scope, element, attrs) { if (attrs.callbackExpression) { var html = $templateCache.get('myDirective.html'); element.replaceWith(html); } }
Be sure to enter $templateCache in your directive:
myApp.directive('myDirective', function ($templateCache) {
Here's a demo using $templateCache : http://jsfiddle.net/ZC4MZ/3/
Option using only a template:
To use a template, you need a variable in the scope. To do this, you can save everything as you have, just add:
link: function(scope, element, attrs) { scope.callbackExpression = attrs.callbackExpression;} }
Demo version of the template / area: http://jsfiddle.net/ZC4MZ/5/