0

I have an ng-if which I know should evaluate to false.. but noticing my directive posSlider is still being initialized via it's function initValues()

page.html

<div ng-if="bigdata.pieces[index].type == 'pos'" class="control-lock pos-detail-container" >
  ...
    <input type="range" min="0" max="100" value="0" class=" vVertical " ng-model="posValue" pos-slider ng-class="{'vclosed':bigdata.pieces[index].state.meta=='open'}"/>
 </div>

Can someone explain what's going on? I thought the ng-if would prevent the directive from initializing until true

directive.js

orion.directive('posSlider', function($ionicGesture, $ionicSlideBoxDelegate, $timeout) {
"use strict";

return {
    restrict :  'A',
    link : function(scope, elem, attrs, e) {
        function initValues() {

            if (scope.detail.state.meta === 'open') {
                scope.posValue = 0;
            }

            else if (scope.detail.state.meta === 'closed') {
                scope.posValue = 100;
            }
        }

        initValues();
   ...
Tyler Eich
  • 4,239
  • 3
  • 21
  • 45
Erik
  • 2,782
  • 3
  • 34
  • 64
  • Check out https://stackoverflow.com/questions/19824121/calling-javascript-within-ng-if – Erick May 26 '15 at 21:37
  • @Erick thank you but this doesn't get at my problem of understanding how angularJS runtime works.. – Erik May 26 '15 at 21:48
  • What do you mean by "initialized"? ngIf is evaluated in its link() function, so if you've got some code in your directive further up the chain, or if your directive is running at a higher priority, then I'd expect it to be executed. – c0bra May 26 '15 at 21:48
  • it is very difficult to understand what is going on here with so little code, as I do not see what `bigdata.peices` is, along with how you are tracking your `ng-repeat` (which I am assuming you are using), but as a guess, did you mean `$index` instead of `index`? – WebWanderer May 26 '15 at 21:52
  • @c0bra added some details for you. when i say initialized, I mean I'm seeing the `posSlider` function getting called. I didn't expect that since it was nested in the `ng-if` – Erik May 26 '15 at 21:52
  • @WebWanderer `bigdata` is merely an object in the global namespace (i didnt write this, bare with me), with `pieces` being an array. `index` is `index`.. no dollar sign. – Erik May 26 '15 at 21:55
  • I would debug by checking the actual value of bigdata.pieces. I tried your use case in a plunker and the it's working like you'd want it to: http://plnkr.co/edit/H3W1bAUtVypSpDf9aLys?p=preview – c0bra May 26 '15 at 22:11
  • 2
    @Erik, that *is* how `ng-if` should work - check with `ng-if="false"`. – New Dev May 27 '15 at 06:20

0 Answers0