5

I try to add conditionally a data attribute value to my vue list on loop and I try the following

<ul data-parent="{{model.parent_id !== null ? model.parent_id : 0}}"></ul>

but in this case the list do not renders anymore, if dump out outside html tag {{model.parent_id !== null ? model.parent_id : 0}} than I see the correct output

1

2 Answers 2

8

Use : before that and I would create a computed property like this.

computed: {
     parentId() {
       if (this.model.parent_id !== null)
          return this.model.parent_id
       return 0;
     }

}

<ul :data-parent="parentId"></ul>
Sign up to request clarification or add additional context in comments.

Comments

4

The right Syntax

<ul :data-parent="{{(model.parent_id !== null) ? model.parent_id : 0}}"></ul>

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.