More than one template in same component in AngularJS 1.5
Asked Answered
D

2

14

Can I use more than one template in AngularJS 1.5 components ? I have one component having one attribute, so I want to load different template based on that attribute name. How can I achieve loading of templates based on attribute name of element?

jsConfigApp.component('show', {
templateUrl: 'component/show.html',  //How to change it based on attribute value?
bindings:{
    view:"@"
},
controller: function () {
    console.log(this.view)
    if (this.view = "user") {
       console.log("user")
    } else if (this.view = "user") {
        console.log("shop")
    } else {
        console.log("none")
    }      
}
})

Thanks.

Dolorisdolorita answered 18/6, 2016 at 22:22 Comment(0)
H
9

I use two ways for making dynamic template of a component in 1.5.x:

1) Pass via attr property:

templateUrl: function($element, $attrs) {
      return $attrs.template;
}

2) Inject a service into template and get template from there:

templateURL function:

templateUrl: function($element, $attrs,TemplateService) {
      console.log('get template from service:' + TemplateService.getTemplate());
      return TemplateService.getTemplate();
}

In getTemplate function return template url based on variable

getTemplate: function(){
     if (this.view = "user") {
          return "user.html";
    } else if (this.view = "user") {
          return "shop.html";
    } else {
        console.log("none")
    } 
    return "shop.html";       
}

Pass variable 'view' to factory firstly via a set method.

If you need more change in html template, back to use directive and use compile service with more support.

Helsa answered 19/6, 2016 at 2:12 Comment(1)
I used this answer as opposed to the more highly voted answer. Using the service method in this answer allows you to avoid hard coding templates. See no-interpolation note by @Blooded below.Vinificator
C
25

What about passing template as an parameter to component? For example create a component like:

module.component('testComponent', {
    controllerAs: 'vm',
    controller: Controller,
    bindings: {
        template  : '@'
    },
    templateUrl: function($element, $attrs) {
        var templates = {
            'first' :'components/first-template.html',
            'second':'components/second-template.html',
            'third' :'components/third-template.html'
        }
        return templates[$attrs.template];
    }
});

And using component as below may help

<test-component template='first'></test-component>
Countrywide answered 19/1, 2017 at 13:49 Comment(2)
This is such an elegant solution. It should be the accepted answer. Thank you!Filiation
this only works if you use 'hard-coded' value of template= attribute inside <test-component> tag. Since you need something like <test-component template='{{templateName}}'></test-component> this solution doesn't work because of no-interpolated valueBlooded
H
9

I use two ways for making dynamic template of a component in 1.5.x:

1) Pass via attr property:

templateUrl: function($element, $attrs) {
      return $attrs.template;
}

2) Inject a service into template and get template from there:

templateURL function:

templateUrl: function($element, $attrs,TemplateService) {
      console.log('get template from service:' + TemplateService.getTemplate());
      return TemplateService.getTemplate();
}

In getTemplate function return template url based on variable

getTemplate: function(){
     if (this.view = "user") {
          return "user.html";
    } else if (this.view = "user") {
          return "shop.html";
    } else {
        console.log("none")
    } 
    return "shop.html";       
}

Pass variable 'view' to factory firstly via a set method.

If you need more change in html template, back to use directive and use compile service with more support.

Helsa answered 19/6, 2016 at 2:12 Comment(1)
I used this answer as opposed to the more highly voted answer. Using the service method in this answer allows you to avoid hard coding templates. See no-interpolation note by @Blooded below.Vinificator

© 2022 - 2024 — McMap. All rights reserved.