1

I am using ng-repeat to display an array and call a function to display variabled.

In my case the function returns 1, 2 or 3. Based on the number i need to display the text say

if 1 then display result as begineer, if 2 then display result as moderate, if 3 then display result as expert.

I am aware of ng-show but is there a quick solution for this.

{{getdetails("skilllevel",course.courseID)}}`

1 Answer 1

3

There are a few ways you could do this in angularjs 1.1.5 and above:

ngSwitch is another option

<div ng-repeater="course in courses">
    <div ng-switch on = course.courseID">
        <p ng-switch-when = "1">beginner</p>
        <p ng-switch-when = "2">moderate</p>
        <p ng-switch-when = "3">expert</p>
    </div>
</div>

Another to use is ng-if.

<div ng-repeater="course in courses">
    <div ng-if = "course.courseID == 1">
        <p>beginner</p>
    </div>
    <div ng-if = "course.courseID == 2">
        <p>moderate</p>
    </div>
    <div ng-if = "course.courseID == 3">
        <p>expert</p>
    </div>
</div>

If you really don't want to use ng-shows and only have two option I would use a ternary if

<div ng-repeater="course in courses">
    <p>{{course.courseID == 1 ? 'beginner' : 'expert'}}</p>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I completed that job with other logics. I shall keep this logic to apply on other projects.

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.