0
<span ng-repeat="sport in profile.sports track by $index">
      {{ (profile.type == 2) ? ($index >= 0) ? sports[sport.sport_id] + ", " : sports[sport.sport_id] : '' }}
</span>

I want each element to be separate by , if there is at least 1 item.

Expected result in sports[sport.sport_id]:

cricket, football, hockey
cricket, hockey
cricket
cricket, soccer

Currently I'm getting all these without commas, please suggest, thanks.

1
  • 1
    Can you show how the $scope.profile looks like? Commented Sep 13, 2017 at 8:07

2 Answers 2

1

You can use javascript join with comma separated

Example

<span ng-repeat="sport in [{value: ['a']}, {value: ['b','c']}]">
      <pre>{{sport.value.join(', ')}}</pre>
</span>

Ouput:

a
b, c
Sign up to request clarification or add additional context in comments.

4 Comments

In my case what will be the exact code as i Use custom filter but it does not work
filter('joinBy', function () { return function (input,delimiter) { return (input || []).join(delimiter || ','); } });
{{sports[sport.sport_id] | joinBy:','}}
Error: (intermediate value).join is not a function
1

try this

<span ng-repeat="sport in profile.sports track by $index">
     {{ modifiedSport(sport, $index) }}
</span>

and add this to your controller

$scope.modifiedSport = function(sport, idx){
    ($scope.profile.type == 2) ? (idx >= 0) ? $scope.sports[sport.sport_id] + ", " : $scope.sports[sport.sport_id] : ''
}

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.