Rohit Ghotkar
Rohit Ghotkar

Reputation: 803

Angular Js , Check substring in comma separated string in angular js

I have got following result from localstorage,

HD, TA, DI

Now In angular js I want to display divs based on this data as:

HD      : Only display Home Delivery Button.
HD, TA  : Display Home Delivery and Take Away Buttons.
HD,TA,DI: Display Home Delivery, Take Away, dine In Buttons.

<div ng-if="result==What should be here"></div>

Upvotes: 3

Views: 1796

Answers (2)

F.Igor
F.Igor

Reputation: 4380

You can use directly in ng-if ($scope.items='HD, TA, DI')

  <button ng-if="items.indexOf('HD')>=0">HD</button>
  <button ng-if="items.indexOf('TA')>=0">TA</button>
  <button ng-if="items.indexOf('DI')>=0">DI</button>

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

Upvotes: 3

Larry Turtis
Larry Turtis

Reputation: 1916

In your controller:

app.controller('MainCtrl', function($scope) {
  $scope.localStorage = ["HD", "TA", "DI"];
  if ($scope.localStorage.indexOf("HD") >= 0) {
    $scope.hd = true;
  }
  if ($scope.localStorage.indexOf("TA") >= 0) {
    $scope.ta = true;
  }
  if ($scope.localStorage.indexOf("DI") >= 0) {
    $scope.di = true;
  }
});

And in your view:

<html ng-app="myApp">
      <body ng-controller="MainCtrl">
        <div ng-if="hd">HD</div>
        <div ng-if="ta">TA</div>
        <div ng-if="di">DI</div>
      </body>
</html>

Upvotes: 1

Related Questions