gpt4 book ai didi

javascript - 添加默认值以使用 Angular 2 选择

转载 作者:行者123 更新时间:2023-11-30 21:05:22 25 4
gpt4 key购买 nike

我正在使用 Visual Studio 2017 ASP.NET Angular Web 应用程序模板开发带有 ASP.NET Core 2.0 的 Angular 2 应用程序。

我有一个 Controller ,其方法 get 返回我在我的应用程序中使用的所有法规(它们来自枚举):

[Route("api/[controller]")]
public class LawController : Controller
{
[HttpGet]
public IEnumerable<Models.LawPresentation> Get()
{
List<LawPresentation> laws = new List<LawPresentation>();
// Arrange
byte[] values = (byte[])Enum.GetValues(typeof(LawTypes));
foreach (byte value in values)
{
string enumName = Enum.GetName(typeof(LawTypes), value);
LawPresentation law = new LawPresentation()
{
Value = value,
Name = enumName
};

laws.Add(law);
}

return laws;
}
}

我这是在选择中显示所有这些值的组件:

<p *ngIf="!laws"><em>Loading...</em></p>
Legislaciones:&nbsp;
<select>
<option *ngFor="let l of laws" [ngValue]="l">{{l.name}}</option>
</select>

我不认为从 Controller 返回默认值(一个空白选项)是个好主意,所以我想知道我是否可以向 html 组件添加一些东西来添加默认选项。

有什么办法可以把select的默认选项添加到html组件中吗?

最佳答案

我会做这样的事情:

<p *ngIf="!laws"><em>Loading...</em></p>
Legislaciones:&nbsp;
<select>
<option></option>
<option *ngFor="let l of laws" [ngValue]="l">{{l.name}}</option>
</select>

关于javascript - 添加默认值以使用 Angular 2 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46682177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com