Angular orderBy number sorting as text in ng-repeat

前端 未结 7 1452
天命终不由人
天命终不由人 2020-11-29 08:10

I have this data:

[{\"id\":\"42\",\"firstname\":\"Sarah\",\"lastname\":\"Dilby\",\"age\":\"40\",\"cars\":\"Yaris\"},
{\"firstname\":\"Jason\",\"lastname\":\"         


        
7条回答
  •  隐瞒了意图╮
    2020-11-29 08:36

    Inside your ng-repeat directive you are using a number filter

    {{person.id | number}}
    

    Filters are used to format the output, but they don't update the Model properties. For example: person.id = 1234.56789 will be rendered as 1,234.568.

    As mentioned above you have to convert age to type Number. Then orderBy will work as it should. For example inside your service:

    angular.forEach(People.details, function (detail) {
      detail.age = parseFloat(detail.age);
    });
    

提交回复
热议问题