如何从ui-select multiple中获取选定的选项

Joh*_*ohn 2 ui-select

我在弄清楚如何从ui-select. 我做了一个片段来告诉你我想做什么。在我的 html 中,我做了一个带有 ng-change-event 回调的 ui-select: ng-change="onDatasetChanged(whatShouldBehere?)"。选择该选项后,我只想onDatasetChanged()在控制器的-method内打印所选模型。

angular.module('myApp',['ui.select']).controller('MyController', function ($scope) {
  
  $scope.myUiSelect={model:{}}; // encapsulate your model inside an object.
  $scope.availableData=["a","b","c"]; //some random options
  
  $scope.onDatasetChanged = function(selectedValue){
    console.log("selectedValue",selectedValue);
  }
  
});
Run Code Online (Sandbox Code Playgroud)
<link href="https://rawgit.com/angular-ui/ui-select/master/dist/select.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://rawgit.com/angular-ui/ui-select/master/dist/select.js"></script>
<body ng-app="myApp" ng-controller="MyController">
  
  
  
  <ui-select multiple ng-model="myUiSelect.model" close-on-select="false" title="Choose a dataset" ng-change="onDatasetChanged(whatShouldBehere?)">
    <ui-select-match placeholder="Select something">{{$item.label}}</ui-select-match>
      <ui-select-choices repeat="data in availableData | filter:$select.search">
                        {{data}}
      </ui-select-choices>
  </ui-select>
  
  
  
</body>
Run Code Online (Sandbox Code Playgroud)

Joh*_*ohn 5

后一点对于库页上的研究ui-select-directive 我想你可以使用一个on-select事件像这样绑定: on-select="onSelect($item, $model)"。查看更新的片段:

angular.module('myApp',['ui.select']).controller('MyController', function ($scope) {
  
  $scope.myUiSelect={model:{}}; // encapsulate your model inside an object.
  $scope.availableData=["a","b","c"]; //some random options
  
  $scope.onSelect = function(item,model){
    console.log("selectedItem",item);
  }
  
});
Run Code Online (Sandbox Code Playgroud)
<link href="https://rawgit.com/angular-ui/ui-select/master/dist/select.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://rawgit.com/angular-ui/ui-select/master/dist/select.js"></script>
<body ng-app="myApp" ng-controller="MyController">
  
  
  
  <ui-select multiple ng-model="myUiSelect.model" close-on-select="false" title="Choose a dataset" on-select="onSelect($item, $model)">
    <ui-select-match placeholder="Select something">{{$item.label}}</ui-select-match>
      <ui-select-choices repeat="data in availableData | filter:$select.search">
                        {{data}}
      </ui-select-choices>
  </ui-select>
  
  
  
</body>
Run Code Online (Sandbox Code Playgroud)