Inside an element directive
named tabset
, i am using element directive
named tab
twice which requires tabset
.
I am doing a console.log(attr.newvar)
from link
of tabset
.
newvar
is the value passed into the scope of the tabset
directive.
So the tabset
gets called 2 times (which i suppose is correct), and hence output is console
d twice.
1st time, the console
output is giving the correct output, but the 2nd time it is showing newvar
as undefined .
but i am not able to access newvar
through scope.newvar
.In case of console.log(scope.newvar)
, i get output as undefined
twice.
Why is this happening ?
HTML snippet
<tabset newvar="black">
<tab></tab>
<tab></tab>
</tabset>
JS snippet
.directive('tab',function(){
return{
restrict:'E',
require:'^tabset',
transclude:true,
scope:{
heading:"@"
},
template:'<div ng-show="active" ng-transclude></div>',
link:function(scope,elem,attr,tabsetCtrl){
scope.active = false;
tabsetCtrl.add(scope);
}
}
})
.directive('tabset',function(){
return{
restrict:'E',
scope:{
item:"=",
newvar:"@"
},
transclude:true,
templateUrl:'/partials/tabset/tabset.html',
bindToController:true,
controllerAs:'tabset',
controller:function($scope){
var self = this;
self.tabs = []
self.add = function add(tab){
self.tabs.push(tab);
if(self.tabs.length === 1){
tab.active = true;
}
}
self.click = function click(selectedTab){
angular.forEach(self.tabs,function(tab){
if(tab.active && tab !== selectedTab)
tab.active = false;
})
selectedTab.active = true;
}
},
link:function(scope,elem,attr,optionsCtrl){
console.log(scope.newvar)
scope.resetInput = function(){
console.log("in resetInput")
optionsCtrl.firstBox = "e"
scope.item = "";
}
}
}
})
tabset template
<ul class="nav nav-tabs" ng-class="'{{newvar}}'" >
<li class='' ng-repeat="tab in tabset.tabs" >
<a href="" ng-click="tabset.click(tab);resetInput();" ng-class='{"tab-active":tab.active,"tab-inactive":tab.active === false}'> {{tab.heading}}</a>
</li>
</ul>
<ng-transclude>
</ng-transclude>
You can use
scope.tabset
to reference the controller and thereby give you access to your variable. Like this:As you are using
bindToController: true
you could access you controller scope from link function 4th parameter which is controller, which will give you access to the directive controllerthis
which is usingcontrollerAs
syntax in it.Update
As you want to add class to your
tab
ul
element. I think you shouldn't useng-class
there.ng-class
used when conditionallyshow
/hide
any class in html. You should use plane{{}}
interpolation in you class attribute. While accessing scope variable you need to usetabset.
because you are usingcontrollerAs
syntax with its alias. I tried to add class withng-attr-class
but the class gets added but other two classes was getting removednav nav tabs
that's the reason behind using{{tabset.newvar}}
.Template
Working Plunkr