1

I am returning an array of data like such:

"Tools": [
    {
      "name": "Online Enrollment",
      "descr": "Allows participants to enroll in benefits for future plans",
      "position": 3,
      "isOn": true,
      "alert": null
    },

What I am trying to get it to do is simply render the div is the "isOn" is set to true. Wouldn't think I would have to do this in an ng-repeat since I am laying it out in the code for each scenario as shown below (as each div contains different text and graphic):

        <div ng-if="pptTools.isOn ==true" class="toolTile col-md-3">
            <a href="#/claimEnter">
                <img src="ppt/assets/toolIcons/submiticon.svg" >
                <p>Submit a Claim for Reimbursement</p>
            </a>
        </div>
        <div ng-if="ppt.Tools.isOn==true" class="toolTile col-md-3">
            <a href="#/commuterOrder">
                <img src="ppt/assets/toolIcons/commutericon.svg" >
                <p>GoNavia Commuter</p>
            </a>
        </div>
        <div ng-if="ppt.Tools.isOn==true" class="toolTile col-md-3">
            <a href="#/accessHsa">
                <img src="ppt/assets/toolIcons/hsa.svg" >
                <p>Access my HSA</p>                        
            </a>
        </div>        
        <div ng-if="ppt.Tools.isOn==true" class="toolTile col-md-3">
            <a href="#/clearSwipe">
                <img src="ppt/assets/toolIcons/clearswipe.svg" >
                <p>Clear a Swipe</p>                        
            </a>
        </div>   

Seems to check the ng-if and tries to render in dev tools, but no div is show. Here is a screenshot of that:

enter image description here

Any help on this please?

Thanks much.

1
  • It would be very useful if you could provide a link for a plnkr or jsfiddler Commented Sep 2, 2015 at 17:33

3 Answers 3

2

You should return an object instead of an array:

"Tools": {
    onlineenrollment: {
       "name": "Online Enrollment",
       "descr": "Allows participants to enroll in benefits for future plans",
       "position": 3,
       "isOn": true,
       "alert": null
    },
    ...
}

Then you can check each item without the ng-repeat

<div ng-if="ppt.Tools.onlineenrollment.isOn" class="toolTile col-md-3">
    <a href="#/clearSwipe">
        <img src="ppt/assets/toolIcons/clearswipe.svg" >
        <p>Clear a Swipe</p>                        
    </a>
</div> 

Alternatively you can reference items in an array with their index, but you shouldn't unless you are completely confident that the order will be consistent every time. I wouldnt do this...

<div ng-if="ppt.Tools[0].isOn" class="toolTile col-md-3">
    <a href="#/clearSwipe">
        <img src="ppt/assets/toolIcons/clearswipe.svg" >
        <p>Clear a Swipe</p>                        
    </a>
</div> 

You could also check out https://docs.angularjs.org/api/ng/function/angular.forEach

Sign up to request clarification or add additional context in comments.

Comments

0

I believe you need a filter. You can pass this array to your filter and use it in ng-repeat. Let's say this array is saved in the controller as $scope.tools and you named your filter as onTools. Then you'd write your code like this (I assumed each tool had attributes for url, img src and name and you want to display them):

<div class="toolTile col-md-3" ng-repeat="tool in tools | onTools">
  <a href="{{tool.url}}">
    <img src="{{tool.imgSrc}}" >
       <p>{{tool.name}}</p>
  </a>
</div>

Comments

0
Try with below code.
---------------------

<div ng-app="myApp">
  <div ng-controller="PatientController">
      <div ng-repeat ="pptTools in pptToolsArray">
    <div ng-if="pptTools.isOn ==true" class="toolTile col-md-3">
            <a href="#/claimEnter">
                <img src="ppt/assets/toolIcons/submiticon.svg" >
                <p>Submit a Claim for Reimbursement</p>
            </a>
        </div>
        <div ng-if="ppt.Tools.isOn==true" class="toolTile col-md-3">
            <a href="#/commuterOrder">
                <img src="ppt/assets/toolIcons/commutericon.svg" >
                <p>GoNavia Commuter</p>
            </a>
        </div>
        <div ng-if="ppt.Tools.isOn==true" class="toolTile col-md-3">
            <a href="#/accessHsa">
                <img src="ppt/assets/toolIcons/hsa.svg" >
                <p>Access my HSA</p>                        
            </a>
        </div>        
        <div ng-if="ppt.Tools.isOn==true" class="toolTile col-md-3">
            <a href="#/clearSwipe">
                <img src="ppt/assets/toolIcons/clearswipe.svg" >
                <p>Clear a Swipe</p>                        
            </a>
        </div> 
            -----
  </div>
</div><div>


var myApp = angular.module('myApp', []);

function SampleController($scope) {
  $scope.pptToolsArray= [{
      "name": "Online Enrollment",
      "descr": "Allows participants to enroll in benefits for future plans",
      "position": 3,
      "isOn": true,
      "alert": null
    },{
      "name": "offline Enrollment",
      "descr": "Allows participants to enroll in benefits for future plans",
      "position": 3,
      "isOn": false,
      "alert": null
    },{
      "name": "No Enrollment",
      "descr": "Allows participants to enroll in benefits for future plans",
      "position": 3,
      "isOn": true,
      "alert": null
    }];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.