3

昨日Angular JSを使い始めたばかりですが、何か明白なことを尋ねている場合は申し訳ありません。私がやろうとしているのは、選択の最初のオプションをデフォルトで選択されているものにすることですが、フロントエンドで注文されているため、間違ったオプションが選択されています。最初のアイテムを選択する前に、コントローラー内からの API 呼び出しから返されたデータを並べ替える必要があると思いますか?

これが私の選択です:

<select ng-model="clientsList" ng-options="c.Name for c in clients | orderBy:'Name'"></select>

これが私のコントローラーです:

function MyCtrl($scope, $http) {
    $scope.init = $http.jsonp('http://MY-API?callback=JSON_CALLBACK')
    .then( function ( response ) {
        $scope.clients = response.data;
        // need to select something, unfortunately, this won't be the first option on the front end because it's re-ordered alphabetically there
        $scope.clientsList = $scope.clients[0];
    });
}
4

1 に答える 1

5

同じフィルターを使用して、コントローラーでデータを並べ替えるだけです。

function MyCtrl($scope, $http, orderByFilter) {
  $scope.init = $http.jsonp('http://MY-API?callback=JSON_CALLBACK')
  .then( function ( response ) {
    $scope.clients = orderByFilter(response.data, 'Name');
    $scope.clientsList = $scope.clients[0];
  });
}

<select ng-model="clientsList" ng-options="c.Name for c in clients"></select>

注: 次の表記法を使用して、コントローラー内に AngularJS フィルターを挿入できます。

[filterName]Filter
于 2013-06-25T11:18:57.750 に答える