ng-repeat show only last element

前端 未结 3 1999
耶瑟儿~
耶瑟儿~ 2021-02-12 16:51

I am developing inbox for messages with AngularJs, and I came across one issue: I want to show only last element within ng-repeat. I did some research, and found ou

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-12 17:23

    use angularjs $last in ng-repeat. here is the doc

    {{n}}

    {{n}} when its last repeat $last will be true otherwise its false so you can use ng-show or ng-if with $last.

    here is a sample demo


    UPDATE I think no need of ng-repeat there since you need to show the last element of the array (this is same as @Michael P. Bazos's answer), to do that:

    $scope.data = [42, 42, 43, 43, 50];
    
    print the last value as : {{ data[data.length-1] }}
    

    here is the demo

    but if you really need to do with ng-repeat do as this

    [data[data.length-1]] create an array only with last element.

    {{n}}

    demo

提交回复
热议问题