Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you change templateUrl on the fly?

Is it possible to change templateUrl on the fly by passing values in the directive's scope? I want to pass data to controller that will render the page based on the data that passed from the directive

something maybe that looks like that:

<div>     <boom data="{{myData}}" /> </div>   .directive('boom', function {         return {             restrict: 'E',             transclude: true,             scope: 'isolate',             locals: { data: 'bind' },             templateUrl: "myTemplate({{boom}}})" // <- that of course won't work.         }     }); 
like image 211
iLemming Avatar asked Jan 31 '13 18:01

iLemming


People also ask

What is templateUrl in Angular?

When to use templateUrl in Angular applications? When you have a complex view, then it recommended by Angular to create that complex view in an external HTML file instead of an inline template. The Angular component decorator provides a property called templateUrl. This property takes the path of an external HTML file.

How to create a new directive in AngularJS?

Create New Directives In addition to all the built-in AngularJS directives, you can create your own directives. New directives are created by using the . directive function. To invoke the new directive, make an HTML element with the same tag name as the new directive.

How to use custom directive in AngularJS?

AngularJS provides support to create custom directives for following type of elements. Element directives − Directive activates when a matching element is encountered. Attribute − Directive activates when a matching attribute is encountered. CSS − Directive activates when a matching css style is encountered.

What is attrs in AngularJS?

Using attrs you are able to access the attributes defined in your html tag like <fm-rating ng-model="$parent.restaurant.price" symbol="$" readonly="true"> So in this case you will have access to the symbol and readonly attributes.


1 Answers

It is possible, but when your template to be loaded depends on some scope-data you can't use the directive's templateUrl property anymore and you will be obliged to use lower-level API, namely $http and $compile.

Roughly what you need to do (only possible in the linking function) is to retrieve template's content using $http (don't forget to involve $templateCache!) and then compile template's content "manually".

It might sound like it is a lot of work but in practice it is rather straightforward. I would suggest having a look at the ngInclude directive sources where this pattern is used.

Here is a skeleton of such a directive:

app.directive('boom', function($http, $templateCache, $compile, $parse) {         return {             restrict: 'E',             link: function(scope , iElement, iAttrs) {                                           var boom = $parse(iAttrs.data)(scope);               $http.get('myTemplate'+boom, {cache: $templateCache}).success(function(tplContent){                 iElement.replaceWith($compile(tplContent)(scope));                               });                           }          }     }); 

assuming that it would be used as <boom data='name'></boom>. Working plunk here: http://plnkr.co/edit/TunwvhPPS6MdiJxpNBg8?p=preview

Please note that I've changed attributes evaluation from {{name}} to attributes parsing since probably a template should be determined only once, at the beginning.

like image 149
pkozlowski.opensource Avatar answered Sep 23 '22 07:09

pkozlowski.opensource