如何更新ng-repeat中的第一项?

时间:2021-01-31 19:33:24

Trying to get my head around ng-repeat and sort! I have a list of JSON objects as follows:

试着让我的头转到ng-repeat和sort!我有一个JSON对象列表,如下所示:

Current user list: [{user: abc, name: ABC},{user: xzy, name: XZY}]

当前用户列表:[{user: abc, name: abc},{user: xzy, name: xzy}]

I've got an ng-repeat that iterates the list to display the user details. This all works, however, I want the first item in the list to be that of the logged in user. In this case user xzy is the logged in user so that person should display first.

我有一个ng-repeat,迭代列表以显示用户详细信息。但是,这一切都是可行的,我希望列表中的第一个项目是登录用户。在这种情况下,用户xzy是登录用户,所以用户应该首先显示。

Any help is appreciated.

任何帮助都是感激。

J

J

1 个解决方案

#1


2  

Use a custom orderBy function and put the current user first, ex:

使用自定义的orderBy函数,并将当前用户放在第一位:

<div ng-repeat="user in users | orderBy: currentUser"></div>

And the function

和功能

$scope.currentUser = function(user) {
    if (user.user == $scope.currentUserName) return 0
    else return 1;
}

Where $scope.currentUserName holds the user name of the currently logged in user.

在美元范围。currentUserName保存当前登录用户的用户名。

#1


2  

Use a custom orderBy function and put the current user first, ex:

使用自定义的orderBy函数,并将当前用户放在第一位:

<div ng-repeat="user in users | orderBy: currentUser"></div>

And the function

和功能

$scope.currentUser = function(user) {
    if (user.user == $scope.currentUserName) return 0
    else return 1;
}

Where $scope.currentUserName holds the user name of the currently logged in user.

在美元范围。currentUserName保存当前登录用户的用户名。