javascript - When does ngClass evaluate functions / How to mimic this in custom directive -
I'm hoping someone can provide me some connoisseur insight on which I'm getting a lot less.
ngClass allows you to add / remove a class based on some functions. For example, if I write:
& lt; Div ng-class = "{'few-square': myfunction ()}" & gt; & Lt; / Div & gt; Again. Myfunction () to evaluate the authenticity or false if some class will be added / removed. What's particularly good is that I do not have to worry about this function, which is called elsewhere to evaluate in my code. NgClass just takes care of keeping the class up to date with the current return value of its functions ().
So my first question is how does the NG class decide when the return value of my function () has changed? Does it check my function () on each digest loop? Probably a clock has been established on this, but what is being seen? I tried to read the code for the NG class, but I did not have enough background knowledge to handle it.
The reason why it wants to know is that I want to duplicate this behavior in a custom. So in my app I have something like NG Class above Myfunction () is sitting on the main controller for my page, but it does a bunch of direct dom manipulation, so it should not be there. I want to move my function out of the controller and move it to a custom command, but if I do this then I need to check with the function again and again to set up the classes. Would like to be able to do something:
app.directive ('myDirective', function () {return {restrict: 'a, link: function (scope, elem, attrs)) {Function myFunction () {// ...} // I want to check it // at the same frequency that the ng cl Number is Czech // Is it all digest the cycle? If (myFunction ()) {elem.addClass ('some-square');} and (elem.removeClass ('some-class');}}} obviously this work It's only going to be running when the instructions are added. So how can I see my work for changes?
I $ Watch , which evaluates the initial argument during each $ digest cycle, and invites listener (second argument) during initial load And after See
you return a different value in the assessment of the initial logic. Base on the psuedo-code provided to you (the actual code testing will be good for ing purposes :-) Here's an example of what you should do:
app.directive ('myDirective', function () {return {restrict: 'a, link: function (scope, elem), ethers) {Scope. $ Watch (function () {//. This is your "myfunction ()" return result;}, function (newrzult) {if (newResult) {elem.addClass ('some-square');} and (Elem.removeClass ('few-square'); }}); }}}
Comments
Post a Comment