簡體   English   中英

有沒有辦法在 html 標簽內的 blazor 組件中執行代碼?

[英]Is there a way to execute code in blazor component inside html tags?

我想做的是這樣的:

<input type="text"
@if(!String.IsNullOrEmpty(Element.Placeholder))
{
   placeholder="@Element.Placeholder"
}
/>

我想用 maxlength、value 等許多屬性來做到這一點。我想出的最好的解決方案是使用 MarkUpString 執行上述操作,然后渲染它。 我想知道是否有一種方法可以在 html 標記內使用 if's,這樣我的代碼會更干凈,更容易修改。 請寬容,我是編程新手,尤其是 Blazor。 謝謝

*編輯:我希望這個組件也可以從另一個組件編輯。

您可以使用@attributes 我在下面展示了一個非常簡單的代碼示例來設置樣式。

<button @attributes="this.ButtonAttributes">Red Button</button>
@code{

  private Dictionary<string, object> ButtonAttributes = new Dictionary<string, object>();

  protected override Task OnInitializedAsync()
  {
    ButtonAttributes.Add("class", "btn btn-danger");
    return Task.CompletedTask;
  }

}

暫無
暫無

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

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