Using ng-if as a switch inside ng-repeat?
Asked Answered
R

3

71

I am working on Angular app. I tried to use ng-if and switch inside ng-repeat but didn't succeed. I have data like:

   **[{"_id":"52fb84fac6b93c152d8b4569",
       "post_id":"52fb84fac6b93c152d8b4567",
       "user_id":"52df9ab5c6b93c8e2a8b4567",
       "type":"hoot",},  
      {"_id":"52fb798cc6b93c74298b4568",
       "post_id":"52fb798cc6b93c74298b4567",
       "user_id":"52df9ab5c6b93c8e2a8b4567",
       "type":"story",},        
      {"_id":"52fb7977c6b93c5c2c8b456b",
       "post_id":"52fb7977c6b93c5c2c8b456a",
       "user_id":"52df9ab5c6b93c8e2a8b4567",
       "type":"article",},**

$scope.comments = data mentioned above
and my Html like :

   <div ng-repeat = "data in comments">
      <div ng-if="hoot == data.type">
          //differnt template with hoot data
       </div>
      <div ng-if="story == data.type">
          //differnt template with story data
       </div>
       <div ng-if="article == data.type">
          //differnt template with article data
       </div> 
   </div>

How can I achieve this thing in Angular?

Razzledazzle answered 13/2, 2014 at 10:35 Comment(0)
P
90

Try to surround strings (hoot, story, article) with quotes ':

<div ng-repeat = "data in comments">
    <div ng-if="data.type == 'hoot' ">
        //different template with hoot data
    </div>
    <div ng-if="data.type == 'story' ">
        //different template with story data
    </div>
    <div ng-if="data.type == 'article' ">
        //different template with article data
    </div> 
</div>
Pupillary answered 13/2, 2014 at 10:40 Comment(3)
Just to point out the difference (since I didn't see it immediately): the answer is to surround the Strings with quotes. Using them as direct literals won't work.Fantinlatour
Is this above solution got worked.... ??? i tried the same but it doesn't get worked :( please help how to work with ng-if to check with literalsDisuse
This doesn't work for me either. It still shows even with ng-if='false' and ng-show='false'Tew
P
85

This one is noteworthy as well

<div ng-repeat="post in posts" ng-if="post.type=='article'">
  <h1>{{post.title}}</h1>
</div>
Phonics answered 10/1, 2015 at 20:19 Comment(3)
ng-if in ng-repeat is HelpfulHasa
This doesn't work at least for angular 1.2: github.com/angular/angular.js/issues/3584.Cherilyncherilynn
This helped me get rid of empty divs.Rube
K
12

I will suggest move all templates to separate files, and don't do spagetti inside repeat

take a look here:

html:

<div ng-repeat = "data in comments">
    <div ng-include src="buildUrl(data.type)"></div>
 </div>

js:

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {

  $scope.comments = [
    {"_id":"52fb84fac6b93c152d8b4569",
       "post_id":"52fb84fac6b93c152d8b4567",
       "user_id":"52df9ab5c6b93c8e2a8b4567",
       "type":"hoot"},  
    {"_id":"52fb798cc6b93c74298b4568",
       "post_id":"52fb798cc6b93c74298b4567",
       "user_id":"52df9ab5c6b93c8e2a8b4567",
       "type":"story"},        
    {"_id":"52fb7977c6b93c5c2c8b456b",
       "post_id":"52fb7977c6b93c5c2c8b456a",
       "user_id":"52df9ab5c6b93c8e2a8b4567",
       "type":"article"}
  ];

  $scope.buildUrl = function(type) {
    return type + '.html';
  }
});

http://plnkr.co/edit/HxnirSvMHNQ748M2WeRt?p=preview

Karakoram answered 13/2, 2014 at 11:27 Comment(5)
This seems to be nice. I am building this with backend Laravel. I am just exploring Angularjs. Is this would be possible with laravel blade templating? if you have tried it with frameworks?Razzledazzle
unfortunatelly, I'm not familiar with laravel. but,anyway, as long as it's angular code, angular will handle it in a sweet way for sureKarakoram
Alright thanks for help. , definitely i would give a try to this methodRazzledazzle
@EugeneP Will angular build the template in the <ng-include /> once and then use it for the ng-repeat? Or will it fetch the new template from ng-include each time?Morganica
@EugeneP I'm going to answer my own question (^) above for anyone else: ng-include's are cached by the browser so an ng-include inside an ng-repeat would be cachedMorganica

© 2022 - 2024 — McMap. All rights reserved.