You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
the problem here is that the template is not compiled. to compile it you'd have to inject $compile service, compile the fetched html template. To link the compiled template with a scope, you'd have to return a linking fn from the compile fn that will keep a reference to the scope. Yeah, it's a lot of work, but it's doable. :-)
Did someone completed solution @IgorMinar suggested?
I have similar problem: I have a directive, which content depends on some data, that should be loaded from resource (full problem is: I what validations/presentations of model elements loaded from backend. So I'll have kind of and type of field is loaded from backend, and only after it is loaded I can decide which template to compile...
For not the only way how to achieve that is try to implement idea, mentioned at @IgorMinar's comment.
@josefernandotolentino you are talking about different templateUrl - the original issue was opened for directives while you are talking about templateUrl being used in a route definition...
Based on @IgorMinar 's comments above, I was able to quickly able to come up with a working solution. Now, all I want to know is whether this is the "right" solution, in terms of efficiency/correctness. Any "gotcha"s that I may have overlooked ?
Off-Topic: By the way, I just went from "heard of AngularJS for the first time" to "learning AngularJS" to "single-handedly fully re-wrote a moderately complex mobile application" in 3 weeks time (which took a team of three 2.5 months to do in jQMobile & Mustache) ! That says a lot for just how fantastic AngularJS is. Big fan right here.
Here's the code. It should be simple enough to imagine the rest of the (controller and template) code -
// activity items can be of several types .. post-photo, status-change, etc. (think facebook activity stream)App.directive('activityItem',['$compile','$http','$templateCache',function($compile,$http,$templateCache){vartemplateText,templateLoader,baseURL='partial/tpl/',typeTemplateMapping={photo: 'photo-activity-item.html',status: 'status-activity-item.html'};return{restrict: 'E',replace: true,transclude: true,scope: {type: '@type',title: '=',authorName: '=',avatar: '=',timeAgo: '=',statsLikes: '=',statsViews: '='// some basic stats for your post},compile: function(tElement,tAttrs){vartplURL=baseURL+typeTemplateMapping[tAttrs.type];templateLoader=$http.get(tplURL,{cache: $templateCache}).success(function(html){tElement.html(html);});returnfunction(scope,element,attrs){templateLoader.then(function(templateText){element.html($compile(tElement.html())(scope));});};}};}])
<!-- simple bootstrap list for, say, all photo posts (hence the hardcoded @type) --><ulclass="nav nav-list"><ling-repeat="post in posts"><activity-itemtype="photo"
title="post.postTitle"
author-name="post.creatorName"
time-ago="post.formattedCreatedTimestamp"
stats-likes="post.likeCount" stats-views="post.viewsCount"
stats-comments="post.threadCount"
avatar="post.creatorAvatarPath" ></activity-item></li></ul>
Reacted by Roman M. Koss, Archakov Dennis and Javier Simbaña
@etchemkay I use a similar method to re-use a form (so no copy-paste) for editing and creating (CRUD), in this context it is editing and creating a calendar event:
app.directive('eventForm',function(){return{restrict: 'E',replace: true,templateUrl: '_form.html',scope: {event: '='// event attribute is binded to the event object of the directive}};});
Note the use of templateUrl inside the directive so you don't have to use $http.get.
@coli As far as I know there is still no way to bind the templateUrl to scope. Which is probably what a lot of people (including myself) were +ing it for.
Is something like this possible? How to provide my own provider for the HTML data. Sorry if it is the wrong place to ask, but it looks closest to what I could find.
No-one seems to have provided an example of the new way to do this so here is a clock directive I wrote using using an optional "template-url" attribute (uses '/app/partials/clock.html' if no attribute is set).
This works in Angular 1.2.4:
Directive:
Directives.directive('clock', function() {
return {
restrict: 'E',
replace: true,
scope: {
// Private scope so that the 1 second tick doesn't trigger all scope digest functions.
},
templateUrl: function(element, attr) { return attr.templateUrl ? attr.templateUrl : '/app/partials/clock.html' },
controller: function($scope, $timeout) {
$scope.$watch(function() { return new Date().getMinutes(); }, function (newValue, oldValue) {
$scope.date = Date.now();
}, true);
setInterval(function(){
$scope.$digest();
}, 1000);
}
}
});
Something that isn't mentioned above is that templateUrl makes an XHR request with Accept headers set to "application / json", this isn't great because it's telling the server that it wants json back when really it wants HTML back.
Is there a way to get templateUrl to set Accept headers to "text/html"?
I have a Question, am new to angularJS," Here i have a directive inside having a template, in the template i was using a templateUrl for rendering the another template', Whats My issue is i Want access the form data inside the template which is called by templateUrl",..Your Answer made my day Thanks Praveen
Why are given continuously example of the link:! None of these examples does not work when the templateUrl: is used. The templateUrl: for the templates is a complex and long code is required.
app.directive('theDirective', function () {
return {
templateUrl: function ($element, $attrs) {
return $attrs.templateUrl || 'some/path/default.html'
},
link: function () {
// here is the handler
}
}
});
Activity
IgorMinar commentedon Jun 12, 2012
not quite what you want but this will work:
the problem here is that the template is not compiled. to compile it you'd have to inject $compile service, compile the fetched html template. To link the compiled template with a scope, you'd have to return a linking fn from the compile fn that will keep a reference to the scope. Yeah, it's a lot of work, but it's doable. :-)
we'll consider this RFE post 1.0
dcu commentedon Jul 21, 2012
+1. many people is asking this feature so what are the main reasons to not implement it?
casio commentedon Oct 9, 2012
+1
abdulazeezsk commentedon Oct 9, 2012
+1
olostan commentedon Oct 31, 2012
Did someone completed solution @IgorMinar suggested?
I have similar problem: I have a directive, which content depends on some data, that should be loaded from resource (full problem is: I what validations/presentations of model elements loaded from backend. So I'll have kind of and type of field is loaded from backend, and only after it is loaded I can decide which template to compile...
For not the only way how to achieve that is try to implement idea, mentioned at @IgorMinar's comment.
teintinu commentedon Nov 8, 2012
Hi Coli, maybe my fork help you: https://github.com/josefernandotolentino/angular.js
Use can use parameters in route templateUrl:
pkozlowski-opensource commentedon Nov 8, 2012
@josefernandotolentino you are talking about different templateUrl - the original issue was opened for directives while you are talking about
templateUrlbeing used in a route definition...teintinu commentedon Nov 9, 2012
ok pkoz, for this reason I said "maybe".
etchemkay commentedon Nov 24, 2012
Based on @IgorMinar 's comments above, I was able to quickly able to come up with a working solution. Now, all I want to know is whether this is the "right" solution, in terms of efficiency/correctness. Any "gotcha"s that I may have overlooked ?
Off-Topic: By the way, I just went from "heard of AngularJS for the first time" to "learning AngularJS" to "single-handedly fully re-wrote a moderately complex mobile application" in 3 weeks time (which took a team of three 2.5 months to do in jQMobile & Mustache) ! That says a lot for just how fantastic AngularJS is. Big fan right here.
Here's the code. It should be simple enough to imagine the rest of the (controller and template) code -
tkrotoff commentedon Nov 24, 2012
@etchemkay I use a similar method to re-use a form (so no copy-paste) for editing and creating (CRUD), in this context it is editing and creating a calendar event:
Note the use of
templateUrlinside the directive so you don't have to use $http.get.For sure the use of
ng-includewould be better:Ideally I would prefer to use directives for JavaScript code and re-use HTML code through
ng-include.jgrund commentedon Dec 14, 2012
+1
ArturGajowy commentedon Dec 17, 2012
+1
ryanzec commentedon Dec 24, 2012
+1
vizo commentedon Jan 6, 2013
+1
lrlopez commentedon Jan 20, 2013
I've proposed an implementation of dynamic templates using functions in #1849. Take a look, may be it fits your needs.
20 remaining items
gregtzar commentedon Aug 30, 2013
@coli As far as I know there is still no way to bind the templateUrl to scope. Which is probably what a lot of people (including myself) were +ing it for.
coli commentedon Aug 30, 2013
Open a new issue please :) this one is long closed.
willemmulder commentedon Oct 3, 2013
As a last note, the ideal declarative way that the first poster describes, is certainly possible!
Only, instead of doing
You can do
Using this simple directive here: https://github.com/willemmulder/loaddirective
Happy coding!
kumarvishal commentedon Oct 30, 2013
+1
Rush commentedon Oct 31, 2013
I am considering using AngularJS and would like to swap its HTTP template fetching for something else:
Is something like this possible? How to provide my own provider for the HTML data. Sorry if it is the wrong place to ask, but it looks closest to what I could find.
unluckypixie commentedon Dec 19, 2013
No-one seems to have provided an example of the new way to do this so here is a clock directive I wrote using using an optional "template-url" attribute (uses '/app/partials/clock.html' if no attribute is set).
This works in Angular 1.2.4:
Directive:
Example usage:
The partial (/app/partials/footer-clock.html):
geoidesic commentedon Mar 26, 2014
Something that isn't mentioned above is that templateUrl makes an XHR request with Accept headers set to "application / json", this isn't great because it's telling the server that it wants json back when really it wants HTML back.
Is there a way to get templateUrl to set Accept headers to "text/html"?
reyraa commentedon Dec 14, 2014
ngInclude implements all $compile functionality itself, I think we can use it inside directive:
<my-directive ng-include="element.template" config="{element.config}"></my-directive>
template is dynamic and configs come as object. I've used this method for a popup with 4 different templates in a project.
shhtephe commentedon Apr 14, 2015
@TR0L, I'm a newbie trying to learn SPA stuff and you saved me so much trouble with your solution! Thank you so much!
praveen1228 commentedon Nov 6, 2015
offwork commentedon Jan 13, 2016
Why are given continuously example of the link:! None of these examples does not work when the templateUrl: is used. The templateUrl: for the templates is a complex and long code is required.
reyraa commentedon Jan 17, 2016
There is a clean way for this:
assuming
appis the angular module:fix(tpl): dont cache prev. invokations template
pankajanupam commentedon Jun 23, 2016
+1