dynamic template in directive

3,353 views
Skip to first unread message

Hao Deng

unread,
Aug 5, 2012, 3:43:36 PM8/5/12
to ang...@googlegroups.com
Hello :
How can I have a dynamic template in a directive?
I would like to have a directive, whose template can be either
'<input> ' or '<select/> depends on instance attribute.
I tried with link : (iElement .. ) -> iElement.replaceWith( $(
'<select ng-model=foo>')) but it will not work because at link stage,
compilation has finished, so ng-model will have no effect.


Thanks in advance.

Hao Deng

unread,
Aug 5, 2012, 4:01:57 PM8/5/12
to ang...@googlegroups.com
Repost my question with [AngularJS] in subject.

Witold Szczerba

unread,
Aug 5, 2012, 7:30:50 PM8/5/12
to ang...@googlegroups.com

Hi,
you can simply compile whatever you want when processing directives. I won't give you specific example as I am writing from my mobile, but take a look at $compile service.

Let us know if you have hard time figuring it out.

Regards,
Witold Szczerba

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.


Hao Deng

unread,
Aug 5, 2012, 10:21:16 PM8/5/12
to ang...@googlegroups.com
I still have trouble.

Here is my html in jade format.
I have 2 test directive, and I would like the first generate a <select> 
while the second generate a <input> 
------------------------------------
doctype 5
html(ng-app='app1')
    head    
        script(src='/lib/angular-1.0.1.js')
        script(src='app1.js')
    body        
        test(ng-model='m1', options='[1,2,3]')
        test(ng-model='m2')
        p m1: {{m1}}
        p m2: {{m2}}
--------------------------

And here is module in coffeescript & js 
---------------
angular.module('app1', [])
.directive 'test', ()->
restrict    : 'E'
template : """ <input> """
replace : true 
-------------------------
(function() {

  angular.module('app1', []).directive('test', function() {
    return {
      restrict: 'E',
      template: " <input> ",
      replace: true
    };
  });

}).call(this);

------------

Inside 'test's function, when returning template: 'foo', there is no instance element or attribute, so there is no way to dynamically return a template. 

Witold Szczerba

unread,
Aug 6, 2012, 7:13:41 AM8/6/12
to ang...@googlegroups.com
Forget about directive:template property and go ahead with link
function. In link function you can ask for a $compile service, use it
to generate a template function and resolve it against current scope.

Hao Deng

unread,
Aug 6, 2012, 7:23:48 AM8/6/12
to ang...@googlegroups.com
Can you have a few line of code sample? 
what do you mean by generate a template function ? 
what do you mean by resolve? 

Witold Szczerba

unread,
Aug 6, 2012, 9:20:21 AM8/6/12
to ang...@googlegroups.com
Here it is:
http://jsfiddle.net/witoldsz/BFpqF/3/

angular.module('app1', []) //
.directive('test', function($compile) {
return {
restrict: 'E',
compile: function(element, attrs) {
var input = angular.element('<input type=text ng-model=myModel>');
return function(scope, element, attrs) {
element.append(input);
var templateFn = $compile(input);
templateFn(scope);
}
}
}
});

--
Witold Szczerba

nme...@gmail.com

unread,
Aug 6, 2012, 10:52:33 AM8/6/12
to ang...@googlegroups.com
Can you use ngSwitch to select between the 2?

Hao Deng

unread,
Aug 6, 2012, 12:47:39 PM8/6/12
to ang...@googlegroups.com
Great thanks Witold, you are so nice. 

Hao Deng

unread,
Aug 6, 2012, 12:49:35 PM8/6/12
to ang...@googlegroups.com
Sounds interesting, will try later. 


--

Lou Campopiano

unread,
Dec 19, 2012, 6:56:18 PM12/19/12
to ang...@googlegroups.com
Hi, i am looking to do something similar to this. In my directive i want to pass the path of a templeteUrl from a json file. Can you tell me how i could accomplish this? I am a newbie to angularjs

Thanks in advance

Witold Szczerba

unread,
Dec 19, 2012, 8:00:09 PM12/19/12
to ang...@googlegroups.com

Hi,
Check the thread about layouts. I have provided an example of such a directive today.

Regards,
Witold Szczerba
---
Sent from my mobile phone.

On Dec 20, 2012 12:56 AM, "Lou Campopiano" <camp...@gmail.com> wrote:
Hi, i am looking to do something similar to this. In my directive i want to pass the path of a templeteUrl from a json file.  Can you tell me how i could accomplish this? I am a newbie to angularjs

Thanks in advance

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.

Witold Szczerba

unread,
Dec 19, 2012, 8:04:31 PM12/19/12
to ang...@googlegroups.com

I mean kind of such a directive, not exactly like this, but it also replaces the template url based on a variable property (by encapsulating ng-include).
You can also use plain ng-include and set the property in controller.

Regards,
Witold Szczerba
---
Sent from my mobile phone.

Lou Campopiano

unread,
Dec 19, 2012, 9:29:44 PM12/19/12
to ang...@googlegroups.com
Can you provide the link to that thread?
Reply all
Reply to author
Forward
0 new messages