0

The function seems to be fine when remove | currency:"£" filter. Is there any option to show only the current user amount ?

(function() {
  var app = angular.module('myApp', []);
  app.controller('roomController', function($scope) {
    $scope.users = [{
      id: 1,
      firstname: 'member',
      spent: 206.98
    }, {
      id: 2,
      firstname: 'member',
      spent: 12.23
    }, {
      id: 3,
      firstname: 'member',
      spent: 302.12
    }, {
      id: 4,
      firstname: 'member',
      spent: 198.21
    }, {
      id: 5,
      firstname: 'member',
      spent: 85.85
    }];
    $scope.currentuser = 4;
  });
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="roomController">
    <h2 ng-repeat="user in users"><i class="fa fa-gbp"></i> {{ user.id === currentuser ? user.spent: '' | currency:"£"}}</h2>
  </div>
</div>

1
  • 1
    It's possible to do it in expression: {{ user.id === currentuser ? (user.spent | currency:"£") : ''}}, but I'd rather use ng-if here. Commented Dec 3, 2014 at 18:27

1 Answer 1

1

If I understand correctly, you don't need the ternary operator, instead put an ng-if in the <h2> that's being repeated.

See below:

(function() {
  var app = angular.module('myApp', []);
  app.controller('roomController', function($scope) {
    $scope.users = [{
      id: 1,
      firstname: 'member',
      spent: 206.98
    }, {
      id: 2,
      firstname: 'member',
      spent: 12.23
    }, {
      id: 3,
      firstname: 'member',
      spent: 302.12
    }, {
      id: 4,
      firstname: 'member',
      spent: 198.21
    }, {
      id: 5,
      firstname: 'member',
      spent: 85.85
    }];
    $scope.currentuser = 4;
  });
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="roomController">
    <h2 ng-repeat="user in users" ng-if="user.id === currentuser"><i class="fa fa-gbp"></i> {{ user.spent | currency:"£"}}</h2>
  </div>
</div>

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

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.