简体   繁体   English

如何获得角材料芯片的选定芯片?

[英]How to get the selected chip of angular-material chips?

You can select the md-chip elements in md-chips by clicking on them, but I haven't found a good method to find out which one got selected in the controller. 您可以通过点击它们来选择md-chipsmd-chip元素,但是我还没有找到一个很好的方法来找出在控制器中选择了哪一个。

Has anyone accomplished this? 有没有人完成这个?

<md-chips ng-model="ctrl.roFruitNames">
  <md-chip-template>
    <strong>{{$chip}}</strong>
    <em>(fruit)</em>
  </md-chip-template>
</md-chips>

http://codepen.io/anon/pen/QbOaLz http://codepen.io/anon/pen/QbOaLz

Use md-on-select : An expression which will be called when a chip is selected. 使用md-on-select:选择芯片时将调用的表达式。

 <md-chips md-on-select="getChipInfo($chip)" md-on-remove="removeChip($chip)"> ... </md-chip>

In your controller 在你的控制器中

$scope.getChipInfo= function(chip_info) {
console.log(chip_info);
}

Unfortunately as far as I can see in Angular Material's code, this is not exposed in the current implementation of md-chip . 不幸的是,就我在Angular Material的代码中看到的而言,这在md-chip的当前实现中并未公开。

You can get around it by accessing the directive's controller directly, but it's quite dirty, and will easily break with future versions of md-chip . 您可以通过直接访问指令的控制器来解决它,但它非常脏,并且很容易打破未来版本的md-chip

<md-chips ng-model="ctrl.roFruitNames" ng-click="ctrl.getSelectedChip($event)">

In the controller: 在控制器中:

self.getSelectedChipIndex = function(event) {       
  var selectedChip = angular.element(event.currentTarget).controller('mdChips').selectedChip;
  alert(selectedChip);
} 

See it working: 看它工作:

http://codepen.io/anon/pen/oXopQq http://codepen.io/anon/pen/oXopQq

There is already an issue in Angular Material requesting something like this, so hopefully it will be added in the future: Angular Material已经有一个问题需要这样的东西,所以希望它将来会被添加:

https://github.com/angular/material/issues/3413 https://github.com/angular/material/issues/3413


[Edit] [编辑]

to fetch chip data: 获取芯片数据:

 var ctrl = angular.element(event.currentTarget).controller('mdChips');

  if(ctrl !== undefined){
     var selectedChip = ctrl.items[ctrl.selectedChip];
  }

Have you tried md-chips' md-on-select ? 你有没有尝试过md-chips的md-on-select In the Codepen you shared you're using Angular Material v0.10, with which md-on-select doesn't work, but if you change it to v0.11.4, it does work: 在Codepen中你分享了你正在使用Angular Material v0.10, md-on-select不能用,但是如果你把它改成v0.11.4,它确实有用:

<md-chips md-on-select="ctrl.select($chip)">

In controller: 在控制器中:

self.select = function(chip) {
  // You got the chip!
}

Here's a forked version of your Codepen: http://codepen.io/anon/pen/vLmKQR 这是Codepen的分叉版本: http ://codepen.io/anon/pen/vLmKQR

MD Chips' API: https://material.angularjs.org/latest/api/directive/mdChips MD Chips的API: https//material.angularjs.org/latest/api/directive/mdChips

Just a side note, if md-on-add doesn't work, use md-on-append instead, although it will be removed on official v1.0 release. 只是旁注,如果md-on-add不起作用,请使用md-on-append ,尽管它将在官方v1.0版本中删除。

您可以使用ng-click调用范围中的函数:

<md-chip-template ng-click="ctrl.doSomething($chip)" >

It should be md-on-add 它应该是md-on-add

<md-chips md-on-add="ctrl.select($chip)">

or 要么

<md-chips md-on-add="yourmodel=ctrl.chipModel">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM