Sajan Sajan - 2 months ago 17
Javascript Question

Angular directive scope value

<tab page="book"> </tab>
<tab page="dial"> </tab>
<tab page="groupe"> </tab>
<tab page="log"> </tab>
<tab page="star"> </tab>


As you can see i have a directive named tab, and this tab have an attribute page,

i need to change the templateUrl based on the value of page attribute


if the page value is
page="abc"
then the templateUrl should be
templateUrl: 'tab/abc.html',



here is my directive code

contactBook.directive('tab', function() {
let m = 'tab/default.html';
return {
restrict: 'E',
scope: {
page: '@',
},
templateUrl: m,
link: function(scope, element, attributes) {
m = "tab/" + scope.page + ".html";
},
};
});


is this logically possible..? or any alternative methode to done this..?

Answer
templateUrl: function(elem,attrs) {
    return "tab/" + attrs.page + ".html" || 'tab/default.html';
},
Comments