For working with Arrays I recommend Lodash or Underscore . It will save you a lot of time in the future. Both have last method:
_.last([1, 2, 3]); //Will return 3
See the links for API and installation. As for viewing the result in the view, you can assign a scope variable in the relevant controller for the result and show it. Controller:
5条答案
按热度按时间lnlaulya1#
ObjectsArr[ObjectsArr.length - 1]
--这将给予数组中的最后一个元素。要在视图中显示它,请执行以下操作:
{{ObjectsArr[ObjectsArr.length - 1]}}
即使数组中没有条目,也可以使用此方法。
z5btuh9x2#
您可以使用ng-if指令并通过以下方式检查元素是否在最后:
请看下面的演示
第一次
oprakyz73#
For working with Arrays I recommend Lodash or Underscore . It will save you a lot of time in the future.
Both have last method:
See the links for API and installation.
As for viewing the result in the view, you can assign a scope variable in the relevant controller for the result and show it.
Controller:
View:
63lcw9qa4#
ES6中的新方法
ObjectsArr.at(-1)
检查MDN/Array/at上的用法和兼容性
8cdiaqws5#
you can use reverse!