从angular2中的json数组中打印单个值

时间:2017-11-08 09:53:34

标签: angular angular2-services

我刚接触到angular2我的问题是iam有json对象,因为我必须只打印第一个id即(期望输出Id = 564),但我在json中得到所有id。我搜索了很多我没有得到一个正确答案,帮我解决这个问题。



<div *ngFor="let x of roilist">
  <p >{{x[0].id}}</p></div>
&#13;
&#13;
&#13;

{"items":[{"id":564,"sku":"MB570Z\/B","name":"Mini DisplayPort to DVI Adapter","attribute_set_id":4,"price":2800,"status":1,"visibility":4,"type_id":"simple","created_at":"2017-08-31 04:49:57","updated_at":"2017-08-31 05:06:31","weight":1,"extension_attributes":[],"product_links":[],"tier_prices":[],"custom_attributes":[{"attribute_code":"description","value":"Every Mac with a Mini DisplayPort allows you to connect an external display or projector using an adapter. You can use an external display as your main workspace or to extend your desktop, or you can work in mirrored mode with a projector so you can view what your audience sees.\r\nUse the Mini DisplayPort to DVI Adapter to connect an advanced digital monitor, such as the 20- or 23-inch Apple Cinema Display, that includes a DVI connector. Review the documentation or check with the manufacturer of your monitor to make sure you're choosing the right adapter."},{"attribute_code":"short_description","value":"Mini DisplayPort to DVI Adapter"},{"attribute_code":"image","value":"\/m\/i\/mini_displayport_to_dvi_adapter.jpg"},{"attribute_code":"small_image","value":"\/a\/p\/apple_hdmi_to_dvi_adapter.jpg"},{"attribute_code":"thumbnail","value":"\/a\/p\/apple_hdmi_to_dvi_adapter.jpg"},{"attribute_code":"options_container","value":"container2"},{"attribute_code":"required_options","value":"0"},{"attribute_code":"has_options","value":"0"},{"attribute_code":"url_key","value":"mini-displayport-to-dvi-adapter"},{"attribute_code":"msrp_display_actual_price_type","value":"0"},{"attribute_code":"tax_class_id","value":"2"},{"attribute_code":"gift_message_available","value":"0"},{"attribute_code":"is_featured","value":"0"},{"attribute_code":"gst_source","value":"28"},{"attribute_code":"hsncode","value":"85444299"},{"attribute_code":"gst_source_after_minprice","value":"-1"}]},{"id":565,"sku":"MB571Z\/A","name":"Mini DisplayPort to Dual-Link DVI Adapter","attribute_set_id":4,"price":9400,"status":1,"visibility":4,"type_id":"simple","created_at":"2017-08-31 04:49:58","updated_at":"2017-08-31 05:06:31","weight":1,"extension_attributes":[],"product_links":[],"tier_prices":[],"custom_attributes":[{"attribute_code":"description","value":"Every Mac with a Mini DisplayPort allows you to connect an external display or projector using an adaptor. You can use an external display as your main workspace or to extend your desktop, or you can work in mirrored mode with a projector so you can view what your audience sees.\nUse the Mini DisplayPort to Dual-Link DVI Adaptor to connect your Mac to a 30-inch display that includes a DVI connector, such as the 30-inch Apple Cinema Display HD, and enjoy the ultimate widescreen canvas with a resolution of 2560 by 1600 pixels."},{"attribute_code":"short_description","value":"Mini DisplayPort to Dual-Link DVI Adapter"},{"attribute_code":"image","value":"\/m\/i\/mini_displayport_to_dual-link_dvi_adapter.jpg"},{"attribute_code":"small_image","value":"\/m\/i\/mini_displayport_to_dual-link_dvi_adapter.jpg"},{"attribute_code":"thumbnail","value":"\/m\/i\/mini_displayport_to_dual-link_dvi_adapter.jpg"},{"attribute_code":"options_container","value":"container2"},{"attribute_code":"required_options","value":"0"},{"attribute_code":"has_options","value":"0"},{"attribute_code":"url_key","value":"mini-displayport-to-dual-link-dvi-adapter"},{"attribute_code":"msrp_display_actual_price_type","value":"0"},{"attribute_code":"tax_class_id","value":"2"},{"attribute_code":"gift_message_available","value":"0"},{"attribute_code":"is_featured","value":"0"},{"attribute_code":"gst_source","value":"28"},{"attribute_code":"hsncode","value":"85444299"},{"attribute_code":"gst_source_after_minprice","value":"-1"}]}],"search_criteria":{"filter_groups":[],"page_size":2},"total_count":92}

2 个答案:

答案 0 :(得分:0)

你应该没有ngFor

  <p >{{roilist[0].id}}</p></div>

答案 1 :(得分:0)

有几种方法可以做到这一点

使用*ngFor

<div *ngFor="let x of roilist let i = index">
  <p *ngIf ="i == 0">{{x[0].id}}</p>
</div>

同样@Sajeetharan建议。

 <p >{{roilist[0].id}}</p></div>