mdChips (feature): value expression
Provide value-expression for mdChips similar to "select" expression in ngOptions. Use case:
- I have a list of values [{ title: 'One', value: 1 }, { title: 'Two', value: 2 }, { title: 'Three', value: 3 }];
- I want to provide value expression to mdChips like "item.value"
- 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]
+1, I think my request (#3863) is a duplicate of this.
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 is correct, this is accomplished by using a Custom Chip Template.
@typotter no, it's not. because you might want to show in template one thing and keep in model something different.
@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
$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?
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 why was an ng repeat expression like that of select not implemented?
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.