2
In an angular application, I designed a small system of tabs and in this system I implemented a directive to create the events of onclick
on each of the tabs. basically the project looked like this:
stylesheet.css
.-tabs{ width: 100%; display: block; margin: 0; padding: 0; }
.-tabs nav { width: auto; display: -ms-flexbox; display: flex; overflow: hidden; }
.-tabs nav div{ height: 42px; flex: 1; display: block; width: auto; float: left; }
.-tabs nav div.active label{ background:#dadada; }
.-tabs nav div label{ width: 100%; height: 42px; padding:0 10px; line-height: 42px;
text-align: center; display: block; background: #d2d2d2;
cursor: pointer; text-overflow:ellipsis;
}
.-tabs nav section{ display: none; }
html
<div class="-tabs" minha-diretiva>
<nav>
<div class="active">
<label>tab 1</label>
<section>{{foo}}</section>
</div>
<div>
<label>tab 2</label>
<section>{{bar}}</section>
</div>
</nav>
<div class="-tabmain"></div>
</div>
app js.
angular.module('app', [])
.directive('minhaDiretiva', function(scope, element, attr, ctrl) {
var el = element[0],
mi = el.querySelectorAll('label');
for(var i=0; i<mi.length;i++){
mi[i].addEventListener('click',function(){
var a = this.parentNode,
b = a.parentNode.querySelectorAll('div'),
c = el.querySelector('.-tabmain');
for (var i = 0; i < b.length; i++) {
b[i].className = b[i].className.replace(' ', ' ');
b[i].className = b[i].className.replace('active', ' ');
};
c.innerHTML = this.parentNode.querySelector('section').innerHTML;
a.className = a.className.concat('active');
},false);
if( mi[i].parentNode.className.indexOf('active') > -1 ){
el.querySelector('.-tabmain').innerHTML = mi[i].parentNode.querySelector('section').innerHTML;
};
};
})
;
My problem is occurring when I update one of ngModel’s foo
or bar
through any function in my controller it seems that they are not updated, on the contrary, the angular cannot even read them in my code block and the block shown is still the ngModel mask. Has anyone ever seen such a mistake? there is something I can do in my directive so that after its processing I call the angular to identify these ngModel’s that are within the same?
Within the directive you will not be able to access these controllers, at least not from the jetio you are using, what you could do is use the concept of father - son, the right is daughter of the controller above, ie. to access the variables above, you could access $Scope. $Parent.foo or $Scope. $Parent.bar.
– Renan Degrandi
Speaks Leandro! To facilitate help, you could put the code involved in http:/jsfiddle.net/?
– Dherik
Got it @Renandegrandi, I was reading a little bit about $apply, there’s no way I can make it after the directive is instituted
$digest
or a$apply
?– LeandroLuk
You can run the link function, it would look like this. Directive('myDirective', Function(Scope, element, attr, Ctrl) { Return ː link: Function (Scope, element, attrs){ ** what you run in here will already work in a APPLY cycle ** } }
– Renan Degrandi