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.
What about passing template as an parameter to component? For example create a component like:
And using component as below may help
I use two ways for making dynamic template of a component in 1.5.x:
1) Pass via attr property:
2) Inject a service into template and get template from there:
templateURL function:
In getTemplate function return template url based on variable
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.