首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从ui-select multiple中获取所选选项

如何从ui-select multiple中获取所选选项
EN

Stack Overflow用户
提问于 2016-12-22 09:29:54
回答 1查看 7K关注 0票数 2

我有一些问题,如何从ui-select中以多个选择形式获得所选的值。我做了一个片段给你看我想做什么。在我的html中,我用ng更改事件回调:ng-change="onDatasetChanged(whatShouldBehere?)"创建了一个ui-select。当选择该选项时,我希望只在我的控制器中的onDatasetChanged()-method中打印选定的模型。

代码语言:javascript
复制
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);
  }
  
});
代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-22 09:43:07

在对-directive的存储库页面进行了一些研究之后,我认为您可以使用on-select事件绑定,例如:on-select="onSelect($item, $model)"。请参阅更新的代码段:

代码语言:javascript
复制
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);
  }
  
});
代码语言:javascript
复制
<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>

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41279803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档