如何在Svelte中动态设置布尔属性?

时间:2017-07-31 01:01:35

标签: svelte

首次尝试:

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

在编译期间导致错误:

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>
                                                                               ^

我能想出的最好的结果:

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

1 个答案:

答案 0 :(得分:1)

Svelte'修复'HTML,以便disabled属性(和其他布尔属性)表现得更加明智:

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

Demo here.