Blazor InputText:有条件地呈现属性 [英] Blazor InputText: conditionally rendering an attribute
本文介绍了Blazor InputText:有条件地呈现属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Blazor vRC1
Blazor vRC1
我正在寻找一种简单的技术,以了解如何在<InputText>
(或与此相关的任何输入组件)中有条件地呈现属性.这在MVC Razor中曾经很简单,您只需在@(...)
语句中编写条件逻辑即可.现在,编写@(...)
在Razor语法中具有不同的含义.
I'm looking for a straightforward technique on how to conditionally render an attribute within an <InputText>
(or any of the input components for that matter). This used to be simple in MVC Razor, where you'd just write the conditional logic within the @(...)
statement. Now, writing @(...)
has different meaning in the Razor syntax.
例如,我想有条件地为InputText
输出autofocus
HTML属性.
For example, I'd like to conditionally output the autofocus
HTML attribute for InputText
.
<InputText
@bind-Value="@TextProperty"
@(MyModel.isAutoFocus ? "autofocus" : "") <- This is invalid razor syntax!
/>
推荐答案
您可以尝试以下代码:
<InputText @bind-Value="@TextProperty" autofocus="@(MyModel.isAutoFocus)" />
请参阅 https://github.com/aspnet/AspNetCore/issues/10122
这篇关于Blazor InputText:有条件地呈现属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文