0
<td ng-repeat="course in student.extracourses">
    <div ng-if="!course">  not selected   </div>
    {{b.note}}
</td>

I have a table with some static columns and dynamic columns with respect to student extracourses. Some students do have extra notes and some do not. How can I show in the table 'not selected' if the 'course' does not exist? I tried this code above and it did not print 'not selected' for those who do not have a course

4
  • I think ng-repeat will only show the student with course and there is no meaning of ng-if inside that. Commented Feb 18, 2019 at 20:33
  • Seems like a logic error. If a student does not have a course then it would probably not be included in their course collection (student.extracourses) Commented Feb 18, 2019 at 20:35
  • @Igor if it does not have it, it returns an empty extracourses Commented Feb 18, 2019 at 20:37
  • That does not make sense (to me). So if there are 4 courses and the student is "enrolled" in 2 of them. Does the array extracourses contain 4 items or just 2? If it is 4 what is the value of the 2 "empty" courses in that collection? (undefined, null, empty object, something else....) Commented Feb 18, 2019 at 20:39

1 Answer 1

1

You can use <div ng-if="course === undefined"> to check whether course is set or not:

<td ng-repeat="course in student.extracourses">
  <div ng-if="course === undefined">not selected</div>
  {{b.note}}
</td>

Or alternatively, check the typeof():

typeof(course) === "undefined"
Sign up to request clarification or add additional context in comments.

2 Comments

I think for the ng-if you meant to write the inverse, otherwise you get "not selected" for everything that is defined. Also the an undefined will evaluate to falsey and an ng-if check on a truethy/falsey expression is perfectly valid.
Yes, I did mean the inverse. My apologies; fixed!

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.