3

ドロップダウン リストから値を選択し、angularJs の対応する選択 ID からデータを取得しようとしています

angular-ui/ui-selectディレクティブを思いついた

<ui-select ng-model="customer" theme="selectize">
     <ui-select-match placeholder="Customer List">{{$select.selected.Name}}</ui-select-match>
        <ui-select-choices repeat="customer in customers | filter: $select.search">
            <span ng-bind-html="customer.Name | highlight: $select.search"></span>
            <small ng-bind-html="customer.Id.toString() | highlight: $select.search"></small>
        </ui-select-choices>
</ui-select>

私のコントローラーでは、顧客メソッドを取得しています

$scope.setActiveCustomer = function(customer) {
  customersService.getCustomers(customer).then(function(response) {
    $scope.selectedCustomer = response;
  });
};

私の問題は

  • setActiveCustomer (onchange など) に使用するイベントを選択したら、顧客を選択します。アクティブ化を制御するために使用してng-click いるため、何度も発生したイベントを使用すると。ng-click

  • 他に、 ui-selectの初期値を設定するにはどうすればよいですか?

ありがとう

4

1 に答える 1

7

ui-select FAQによると、式ng-modelには aが含ま.れている必要があります。たとえば、次のようになります。

<ui-select ng-model="model.customer" theme="selectize">

初期値を設定するには、で指定されたプロパティを設定するだけng-modelでよいため、コントローラーで次のようにします。

$scope.model = {
  customer: $scope.customers[0] // set the initial selected option
};

onchange イベントの場合、次のように使用できます$scope.$watch()

$scope.$watch('model.customer', function (customer) {
  $scope.setActiveCustomer(customer);
});

お役に立てれば。

于 2014-08-15T11:36:45.123 に答える