I am working on my first single page Angular.js application and am kind of stuck at programmatically compilling/evaluating a custom directive in order to insert it into the DOM from within a controller. The custom directive I created uses two values (which return functions and take a parameter) and a parameter. The whole thing works fine with ng-repeat in the initial html of the SPA:
.directive('myDirective', ['value1', 'value2', function('value1', 'value2'){
return {
restrict: 'E',
scope: {
param: '=param'
},
replace: true,
templateUrl: '/path/to/template.html',
link: function(scope, element, attrs){
scope.v1 = value1(scope.param);
scope.v2 = value2(scope.param);
}
};
})
The directive template looks somewhat like this:
<div>
<img ng-src="{{ param.img.src }}" />
<div>
<a href="{{ param.link.src }}">{{ param.link.text }}</a>
<time datetime="{{ v1 }}">{{ v2 | date: 'medium' }}</time>
<span ng-bind-html="param.text | customFilter1 | customFilter2 | customFilter3"></span>
</div>
</div>
Everything works out nicely when I use this directive in the page html like this:
<ul ng-controller="SomeController" ng-cloak>
<li ng-repeat="param in params">
<my-directive param="param"></my-directive>
</li>
</ul>
I now would like to reuse the directive to dynamically generate the respective html programmatically in a function from within a controller.
My approach thereto tried to use the $compile function but without success:
// in controller:
// ...
$scope.generateMyDirective = function(param){
var compiled = $compile('<my-directive param="param"></my-directive>')({});
someElement.innerHTML = compiled[0];
};
The result (i.e. someElement) does include the static html of the directive, but won't evaluate any expressions within the directive, e.g. {{ param.img.src }} or {{ v1 }}. Furthermore the following type error is thrown:
TypeError: scope.$new is not a function
I tried to compile with different scopes, e.g. $scope of the controller or simply true to generate a new scope, but none will evaluate the expressions within the directive template. I also tried to invoke $scope.$apply() after inserting the directive html into someElement, which didn't work either.
I'm kind of stuck at this point, out of ideas and thankful for any hints. I hope you guys can help me fix this.
$compile('...')($scope); You provide empty object instead of scope.$compile('<my-directive param="param"></my-directive>)($scope)seems to bring me a little bit further. However it now seems likeparam, which is part of the method signuature$scope.generateMyDirective = function(param){...}, is not passed to the directive. This leads to errors when compiling the template, e.g.Cannot read property 'img' of undefined. How can I make sure theparampassed to the method is in turn passed to the directive within$compile()?paramto$scopebefore calling$compile()in the controller it leads to every call togenerateMyDirectiveshowing the sameparam, but it won't throw errors. I, however, would prefer to avoid binding the method parameter to the controller scope and I need to be able to compile for differentparams. Is there any other way of passing the parameter to$compileand therefore the directive?