且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

访问 Angular2 模板中的特定数组元素

更新时间:2023-12-01 18:25:16

2020 Edit :

{{elements?.[0].name}} 

是空检查的新方法

原答案:{{elements[0].name}}

Original answer : {{elements[0].name}}

应该可以正常工作.如果你加载 elements 异步(从服务器或类似的),那么当 Angular 在来自服务器的响应到达之前尝试更新绑定时会失败(通常是这种情况).不过,您应该会在浏览器控制台中收到一条错误消息.

should just work. If you load elements async (from a server or similar) then Angular fails when it tries to update the binding before the response from the server arrived (which is usually the case). You should get an error message in the browser console though.

试试吧

{{elements && elements[0].name}}