Dean Jason
Dean Jason

Reputation: 105

multiple expression if else within ng-repeat

<div ng-repeat="book in books">
<a href="#/{{book.title}}"> <h3 class="title">{{book.title}}</h3></a>
</div>

I use above code to get and it work fine, but I also have a search function, which will also set $scope.books. Unfortuunetly I have no control over the api, the search callback returned different scheme, means to get the title of the books I have to navigate to somewhere else, like book.category.item.title.

so I'm thinking of doing something like this

<a href="#/{{book.title || book.category.item.title}}">

check if book.title isset, else display another expression. Is it correct?

Upvotes: 2

Views: 129

Answers (1)

mohamedrias
mohamedrias

Reputation: 18566

Instead I would prefer using ng-href

<a ng-href="{{getTitle()}}">link</a>

In your controller, return the url by checking logic there.

angular.module("app",[])
   .controller("MainCtrl", function($scope) {
      $scope.book = {};
      $scope.book.title = null;
      $scope.book.category = {
        item : {
          title : "Rafi"
        }
      }
      $scope.getTitle = function() {
        return '#/'+ ($scope.book.title || $scope.book.category && $scope.book.category.item && $scope.book.category.item.title);
      }
    });

DEMO

if you don't want to use controller for some reason:

 <a ng-href="{{'#/'+(book.title || book.category.item.title)}}">

Upvotes: 1

Related Questions