Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular directive templateUrl relative to .js file

I'm building an angular directive which will be used in a few different locations. I can't always guarantee the file structure of the app the directive is used in, but I can force the user to put the directive.js and directive.html (not the real file names) in the same folder.

When the page evaluates the directive.js, it considers the templateUrl to be relative to itself. Is it possible to set the templateUrl to be relative to the directive.js file?

Or is it recommended to just include the template in the directive itself.

I'm thinking I may want to load different templates based on different circumstances, so would prefer to be able to use a relative path rather than updating the directive.js

like image 778
pedalpete Avatar asked Jan 13 '14 23:01

pedalpete


People also ask

What is @templateurl in AngularJS?

templateUrl can also be a function which returns the URL of an HTML template to be loaded and used for the directive. AngularJS will call the templateUrl function with two parameters: the element that the directive was called on, and an attr object associated with that element.

Can I create my own directives for AngularJS to use?

Much like you create controllers and services, you can create your own directives for AngularJS to use. When AngularJS bootstrapsyour application, the HTML compilertraverses the DOM matching directives against the DOM elements. What does it mean to "compile" an HTML template?

What is compiling in AngularJS?

For AngularJS, "compilation" means attaching directives to the HTML to make it interactive. The reason we use the term "compile" is that the recursive process of attaching directives mirrors the process of compiling source code in compiled programming languages. Matching Directives

How to access scope variables from the templateurlfunction in AngularJS?

AngularJS will call the templateUrlfunction with two parameters: the element that the directive was called on, and an attrobject associated with that element. Note:You do not currently have the ability to access scope variables from the templateUrlfunction, since the template is requested before the scope is initialized.


1 Answers

The currently executing script file will always be the last one in the scripts array, so you can easily find its path:

// directive.js  var scripts = document.getElementsByTagName("script") var currentScriptPath = scripts[scripts.length-1].src;  angular.module('app', [])     .directive('test', function () {         return {             templateUrl: currentScriptPath.replace('directive.js', 'directive.html')         };     }); 

If you're not sure what is the script name (for example if you're packing multiple scripts into one), use this:

return {     templateUrl: currentScriptPath.substring(0, currentScriptPath.lastIndexOf('/') + 1)          + 'directive.html' }; 

Note: In cases where a closure is used, your code should be outside to ensure that the currentScript is evaluated at the correct time, such as:

// directive.js  (function(currentScriptPath){     angular.module('app', [])         .directive('test', function () {             return {                 templateUrl: currentScriptPath.replace('directive.js', 'directive.html')         };     }); })(     (function () {         var scripts = document.getElementsByTagName("script");         var currentScriptPath = scripts[scripts.length - 1].src;         return currentScriptPath;     })() ); 
like image 153
Alon Gubkin Avatar answered Sep 25 '22 16:09

Alon Gubkin