簡體   English   中英

如何使用 concat 將條件字符串傳遞給 ember 組件屬性?

[英]How to pass conditional string to ember component attribute using concat?

使用 ember concat()方法分配組件屬性時傳遞條件字符串的正確方法是什么?

考慮以下

{{#my-compontent class=(concat (dasherize model.name)"-row")}}
  {{model.name}}
{{/my-component}}

我想要做的是基於model.disabled屬性有條件地添加--disabled后綴。

我試過的:

{{#my-compontent class=(concat (dasherize model.name)"-row{{if model.disabled '--disabled'}}")}}
  {{model.name}}
{{/my-component}}

導致:

<div class="component-name-row{{if model.disabled '--disabled}}">
  Component name
</div>

我嘗試的另一件事是使用ternary助手,基於這個答案

{{#my-compontent class=(concat (dasherize model.name)"-row"(ternary model.disabled '--disabled' ''))}}
  {{model.name}}
{{/my-component}}

但它崩潰了:

斷言失敗:找不到名為“三元”的助手

有沒有辦法做到這一點? 如果不是,我將如何使用不同的方法達到相同的效果?

像往常一樣,詢問后 5 分鍾我自己找到了答案:

{{#my-compontent class=(concat (dasherize model.name)"-row(if model.disabled '--disabled')")}}
  {{model.name}}
{{/my-component}}

線索是在{{if condition 'string'}}上使用(if condition 'string')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM