如何在ng-repeat中制作第一个元素
how to make first element in ng-repeat
我的任务是首先列出用户名。这是例子
$scope.items = [John,Bob,Tanya,Owen,George];
<div ng-repeat="item in items"></div>
结果将是:
John
Bob
Tanya
Owen
George
我怎样才能让欧文首先像:
Owen
John
Bob
Tanya
George
您可以将 orderBy
与 表达式 一起使用。
如果您有一个与此类似的 JSON
对象....
$scope.itemsWithOrder = [{
"name": "Owen",
"order": 1
}, {
"name": "Dave",
"order": 3
}, {
"name": "Ken",
"order": 2
}, {
"name": "John",
"order": 4
}];
然后您可以使用 order
属性 对集合进行排序。
<div ng-repeat="itemw in itemsWithOrder | orderBy :'order'">
{{itemw.name}} - {{itemw.order}}
</div>
这将为您提供输出...(您也可以使用 -
反转输出,给出 orderBy :'-order'
)
Owen - 1
Ken - 2
Dave - 3
John - 4
我知道这对您拥有的对象没有帮助,但您可以从服务器获取替代对象吗?
你可以看看我上面提供的fiddle。我创建了一个函数,它接受一个数组和一个要搜索的字符串。如果找到,它会记下它的索引,然后将它移到数组的前面并删除旧的。
var app = angular.module('myApp', []);
app.controller('myController', function($scope){
$scope.items = ["John","Bob","Tanya","Owen","George"];
$scope.fixArray = function(array, name){
angular.forEach(array, function(a){
if (a == name){
$scope.index = array.indexOf(a);
}
});
array.unshift(array[$scope.index]);
array.splice($scope.index + 1, 1);
};
$scope.fixArray($scope.items, "Owen");
});
请检查这个http://plnkr.co/edit/qcj8lLuUppYzS9ytW0WR?p=preview
简单地做:
<div ng-repeat="item in items| orderBy">{{item}}</div>
控制器
var app= angular.module('app', []);
app.controller('ctrl', function($scope) {
$scope.items = ['John','Bob','Tanya','Owen','George'];
});
我的任务是首先列出用户名。这是例子
$scope.items = [John,Bob,Tanya,Owen,George];
<div ng-repeat="item in items"></div>
结果将是:
John
Bob
Tanya
Owen
George
我怎样才能让欧文首先像:
Owen
John
Bob
Tanya
George
您可以将 orderBy
与 表达式 一起使用。
如果您有一个与此类似的 JSON
对象....
$scope.itemsWithOrder = [{
"name": "Owen",
"order": 1
}, {
"name": "Dave",
"order": 3
}, {
"name": "Ken",
"order": 2
}, {
"name": "John",
"order": 4
}];
然后您可以使用 order
属性 对集合进行排序。
<div ng-repeat="itemw in itemsWithOrder | orderBy :'order'">
{{itemw.name}} - {{itemw.order}}
</div>
这将为您提供输出...(您也可以使用 -
反转输出,给出 orderBy :'-order'
)
Owen - 1
Ken - 2
Dave - 3
John - 4
我知道这对您拥有的对象没有帮助,但您可以从服务器获取替代对象吗?
你可以看看我上面提供的fiddle。我创建了一个函数,它接受一个数组和一个要搜索的字符串。如果找到,它会记下它的索引,然后将它移到数组的前面并删除旧的。
var app = angular.module('myApp', []);
app.controller('myController', function($scope){
$scope.items = ["John","Bob","Tanya","Owen","George"];
$scope.fixArray = function(array, name){
angular.forEach(array, function(a){
if (a == name){
$scope.index = array.indexOf(a);
}
});
array.unshift(array[$scope.index]);
array.splice($scope.index + 1, 1);
};
$scope.fixArray($scope.items, "Owen");
});
请检查这个http://plnkr.co/edit/qcj8lLuUppYzS9ytW0WR?p=preview
简单地做:
<div ng-repeat="item in items| orderBy">{{item}}</div>
控制器
var app= angular.module('app', []);
app.controller('ctrl', function($scope) {
$scope.items = ['John','Bob','Tanya','Owen','George'];
});