且构网

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

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

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

2020

{{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}}