Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a boolean attribute dynamically in Svelte?

Tags:

svelte

First attempt:

<button type="submit" class="button" {{#if isDirty(item)}}disabled{{/if}}>Save2</button>

causes an error during compile:

bundling...
🚨   (svelte plugin) Error: (28:75) Expected >
26:         <div class="row">
27:           <!-- FIXME Can't set an attribute dynamically? -->
28:           <button type="submit" class="button" {{#if isDirty(item)}}disabled{{/if}}>Save2</button>
                                                                               ^

The best I could come up with:

{{#if isDirty(item)}}
  <button type="submit" class="button" disabled>Save</button>
{{else}}
  <button type="submit" class="button">Save</button>
{{/if}}
like image 263
ScottM Avatar asked Jul 31 '17 01:07

ScottM


1 Answers

Svelte 'fixes' HTML so that the disabled attribute (and other boolean attributes) behave more sanely:

<button type="submit" class="button" disabled='{isDirty(item)}'>Save</button>

Demo here.

EDIT: Updated example and Demo to Svelte 3

like image 85
Rich Harris Avatar answered Oct 19 '22 19:10

Rich Harris