material icon indicating copy to clipboard operation
material copied to clipboard

mdChips (feature): value expression

Open smashercosmo opened this issue 11 years ago • 11 comments

Provide value-expression for mdChips similar to "select" expression in ngOptions. Use case:

  1. I have a list of values [{ title: 'One', value: 1 }, { title: 'Two', value: 2 }, { title: 'Three', value: 3 }];
  2. I want to provide value expression to mdChips like "item.value"
  3. So when I add first two items in the model (for example using autocomplete), i'll get the model, that will look like [1,2]

smashercosmo avatar Jul 01 '15 08:07 smashercosmo

heyton avatar Jul 01 '15 08:07 heyton

liyatang avatar Jul 02 '15 10:07 liyatang

+1, I think my request (#3863) is a duplicate of this.

ilanbiala avatar Jul 22 '15 21:07 ilanbiala

You may actually be able to do this with:

<md-chip-template>
  <span>{{$chip.name}}</span>
</md-chip-template>

so you can have an object and display only certain properties. That isn't clear in the docs, but check out the custom inputs demo 3rd example.

ilanbiala avatar Jul 23 '15 14:07 ilanbiala

ilanbiala is correct, this is accomplished by using a Custom Chip Template.

typotter avatar Aug 10 '15 03:08 typotter

@typotter no, it's not. because you might want to show in template one thing and keep in model something different.

smashercosmo avatar Aug 10 '15 07:08 smashercosmo

@smashercosmo: Can you please provide an example? You can put anything into the chip template. If your chip label is not a field of your collection object, but a function of it, you can do that to:

<md-chip-template>
  <span>{{getChipLabel($chip)}}</span>
</md-chip-template>

typotter avatar Aug 10 '15 15:08 typotter

@typotter

$scope.list = [{ title: 'One', value: 1 }, { title: 'Two', value: 2 }, { title: 'Three', value: 3 }];
$scope.model = [];
<md-chip-template>
  <span>{$chip.title}</span>
</md-chip-template>

After adding all three chips, I'll get (One) (Two) (Three) in my view, but I also want to have [1,2,3] in my model. This is the same functionality that you can get with ng-options by using "select as label for value in array" syntax. Does this make sense?

smashercosmo avatar Aug 10 '15 15:08 smashercosmo

If you want the collection model to be [1, 2, 3] instead of [{ title: 'One', value: 1 }, ...], you can provide an md-on-append expression, but you will also need an external function to render the chip label:

md-on-append="$chip.value"

and chip template:

<md-chip-template>
  <span>{{getChipLabel($chip)}}</span>
</md-chip-template>

What you're proposing, at heart, is to get the label for a chip from somewhere external to the chip object, which can be accomplished as demonstrated above. Proving an expression like item.value will not work, since you want your collection to be the integers, and not the objects containing the value field.

Alternatively, you could convert the collection wherever you use it:

myInts = myChips.map(function(chip) { return chip.value; });

typotter avatar Aug 10 '15 16:08 typotter

@typotter why was an ng repeat expression like that of select not implemented?

ilanbiala avatar Aug 10 '15 16:08 ilanbiala

Chips contains only the collection of selected objects and has no knowledge of the available options. This is why such an expression is not used - the list of available options is outside the scope of Chips' concerns.

typotter avatar Aug 10 '15 16:08 typotter