Firenter Firenter - 1 month ago 6
TypeScript Question

Select with ngOptions returns undefined to ngModel

So I'm trying to use a select field with ngOptions to fill it. But whenever I select something the variable I'm using in ngModel gets turned undefined.

Here's my HTML:

<select ng-options="name as operator.name for operator in vm.operators track by operator.value" ng-model="vm.oper"></select>


And my Typescript:

private operators: Array<any> = [
{
name: 'greater than',
value: '>'
},
{
name: 'less than',
value: '<'
},
{
name: 'equal',
value: '='
},
{
name: 'greater than or equal',
value: '>='
},
{
name: 'less than or equal',
value: '<='
}
];

private oper: any = null;


The select field is displaying fine, and the values seem to be correct in the HTML. But when I select any value there "oper" becomes undefined

I don't see what is causing this, it should be simple right?

Answer

Try to use operator instead of name or operator.value

<select ng-options="operator.value as operator.name for operator in vm.operators track by operator.value" ng-model="vm.oper"></select>

or

<select ng-options="operator as operator.name for operator in vm.operators track by operator.value" ng-model="vm.oper"></select>
Comments