0

I am using angular js with multiple conditions all using the same id. Need to get text just from the condition that is true.

I just found a big bug in an app I'm about to release. I am using angularjs/ionic vs 1 and use allot of conditions in my html. These condition produce great results and show what I want to be shown. However whenever I save the information it fetches the element id, which each condition has the same id, and instead of giving me the text to the visible condition, I get all of them. I need a way to grab the id text only if it's visible. This used to work, unless this was missed during testing. Checking the id in controller.

 function JqliteIds(id) {
       // var myElement = angular.element( document.querySelector( '#some-id' ) );
        var p = angular.element(document.getElementById(id)).text().trim();
        p = p.replace(/(\r\n|\n|\r)/gm, " ");
        p = p.replace(/ +/g, ' ');
        return p;
        //stepTwoFormula = stepTwoFormula.replace(/(\r\n|\n|\r)/gm, " ");
        //stepTwoFormula = stepTwoFormula.replace(/ +/g, ' ');
    }

The html form with multiple conditions. The id is... id="stepOneFormula"

     <div class="item item-text-wrap">
        <div ng-if="level.light">
            <div ng-show="level.stepOne.bleachAdditiveType < 0">
                <p id="stepOneFormula">
                    Mix {{config.productTypes[level.stepOne.productType]}} with
                    <span ng-if="level.stepOne.productType === 1 || level.stepOne.productType === 2">
                        the manufacturer suggested developer or
                    </span> {{level.stepOne.peroxideVolume}} volume / {{level.stepOne.peroxidePercentage}} percent peroxide until it is the consistency of mayonnaise.
                </p>
            </div>

            <div ng-show="level.stepOne.bleachAdditiveType > -1">
                <p id="stepOneFormula">
                    Add approximately {{config.partsInches[level.stepOne.bleachInches]}} of
                    {{config.productTypes[level.stepOne.bleachAdditiveType]}} to {{config.productTypes[level.stepOne.productType]}}. Mix with
                    <span ng-if="level.stepOne.productType === 1 || level.stepOne.productType === 2">
                        the manufacturer suggested developer or
                    </span> {{level.stepOne.peroxideVolume}} volume / {{level.stepOne.peroxidePercentage}} percent peroxide until it is the consistency of mayonnaise.
                </p>
            </div>
        </div>
        <div ng-if="!level.light">
            <p>
                <!--Going Red-->
                <div ng-show="level.stepOne.redRootsTonePercentOne > -1">
                    <div id="stepOneFormula" ng-include="'app/formula-result/service-types/double-process/red-rootsDbl1.html'"></div>
                </div>
                <!--Mixed Levels of gray-->
                <div ng-show="level.stepOne.grayTonePercentFour > -1">
                    <div id="stepOneFormula" ng-include="'app/formula-result/service-types/double-process/gray-mixedDbl1.html'" data="level.stepOne"></div>
                </div>
                <!--Regular Levels-->
                <div ng-show="level.stepOne.grayTonePercentFour === -1 && level.stepOne.redRootsTonePercentOne === -1">
                    <div id="stepOneFormula" ng-include="'app/formula-result/service-types/double-process/genericDbl1.html'"></div>
                </div>
            </p>
        </div>
    </div>
Sheri Trager
  • 842
  • 3
  • 13
  • 33
  • 3
    id values must be unique in a document (page). If two elements have the same id value it's like you and somebody else having the same drivers license number. – Pointy Nov 13 '17 at 00:27
  • Possible duplicate of [Check if element is visible in DOM](https://stackoverflow.com/questions/19669786/check-if-element-is-visible-in-dom) – jwebb Nov 13 '17 at 00:28

1 Answers1

0

As soon as I posted this question I started reading on the ng-show, ng-if and changed my ng-shows to ng-if... it worked. https://docs.angularjs.org/api/ng/directive/ngIf

Don't need to check if element is visible. Unlike ng-show, the ngIf directive removes or recreates the element. So if it isn't created it's not there so the id is irrelevant.

Sheri Trager
  • 842
  • 3
  • 13
  • 33