0

I'm looking for the smoothest way to do something like this:

<span x-ng-show="stuff && stuff.id in [1,2,5]">{{stuff.name}}</span>
<span x-ng-show="stuff && stuff.id in [3,4,6]">{{stuff.fullName}}</span>

I know this is not working in AngularJS, but I hope you understand what I'm trying to do. What is the best readable solution for this?

Unfortunately, I needed a solution that also works in IE11

1 Answer 1

1

If I understand you correctly, it should be a simple check if an array contains something

[1,2,5].includes(stuff.id)

Working example

angular.module('app', [])
.controller('ctrl', function($scope) {
  $scope.stuff = {
    id: 5,
    name: 'foo'
  }
  
  $scope.toggle = function() {
    $scope.stuff.id = $scope.stuff.id === 5 ? 8 : 5;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<span x-ng-show="stuff && [1,2,5].includes(stuff.id)">{{stuff.name}}</span>
<button ng-click="toggle()">Toggle id between 5 and 8</button>
<pre>stuff.id = {{stuff.id}}</pre>
</div>

For < IE11

angular.module('app', [])
.controller('ctrl', function($scope) {
  $scope.stuff = {
    id: 5,
    name: 'foo'
  }
  
  $scope.toggle = function() {
    $scope.stuff.id = $scope.stuff.id === 5 ? 8 : 5;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<span x-ng-show="stuff && [1,2,5].indexOf(stuff.id) > -1">{{stuff.name}}</span>
<button ng-click="toggle()">Toggle id between 5 and 8</button>
<pre>stuff.id = {{stuff.id}}</pre>
</div>

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

1 Comment

works in good browsers. But how do I do it so it also works in IE11?

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.