您可以在對象上綁定一個檢查屬性,那麼你的排序依據子句中使用這樣的:
<div ng-controller="Ctrl">
<ul>
<li ng-repeat="fruit in fruits | orderBy:['checked', 'name']">
<label><input type="checkbox" name="{{fruit.name}}" id="{{fruit.name}}" ng-model="fruit.checked">{{fruit.name}}</label>
</li>
<ul>
</div>
你的小提琴是最新的。
編輯:因爲undefined並不嚴格等於false,所以當您選中並取消選中複選框時,您可以按順序出現錯誤。您可以初始化每個數據的檢查屬性或使用自定義函數檢查屬性(我認爲自定義函數更好)。 下面是代碼:
HTML:
<div ng-controller="Ctrl">
<ul>
<li ng-repeat="fruit in fruits | orderBy:[checkedFunction, 'name']">
<label><input type="checkbox" name="{{fruit.name}}" id="{{fruit.name}}" ng-model="fruit.checked">{{fruit.name}}</label>
</li>
<ul>
</div>
控制器:
var app = angular.module('app', []);
function Ctrl($scope) {
$scope.fruits = [
{'name':'apple'},
{'name':'Mary'},
{'name':'Mike'},
{'name':'Adam'},
{'name':'Julie'}
];
$scope.checkedFunction = function(fruit) {
return fruit.checked || false;
};
}
你的小提琴是最新的與第二個解。
「現在我想檢查顯示在上面,然後按字母順序列出」 - 這是唯一有用的句子,但你使它變得模糊。改寫它。 – zsong
@sza OP想要首先按'checked'進行排序,然後按字母順序排序(假設爲'value')。 –