Vuejs append data-attribute conditionally
Asked Answered
T

2

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

Tonie answered 14/9, 2017 at 11:17 Comment(1)
Does this answer your question? VueJS conditionally add an attribute for an elementInefficacious
A
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>
Andriaandriana answered 14/9, 2017 at 11:28 Comment(0)
M
4

The right Syntax

<ul :data-parent="{{(model.parent_id !== null) ? model.parent_id : 0}}"></ul>
Making answered 13/4, 2018 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.