A
A
Arthur2016-03-22 12:30:42
Angular
Arthur, 2016-03-22 12:30:42

How to initialize SELECT with a value and not an object?

the actual subject
plnkr.co/edit/M0C84exBn1Uf6U8xuWxr
is a blank here. where is the mistake?
it is necessary that during initialization in $scope.select was id=1, but at the same time the select was selected.

<select ng-model='select' ng-init="select = types[0].id"
    ng-options='type.id as type.name for type in types track by type.id'></select>
  <div>{{select}}</div>

$scope.types = [{
    id: 1,
    name: 'test1'
  }, {
    id: 2,
    name: 'test2'
  }, {
    id: 3,
    name: 'test3'
  }, {
    id: 4,
    name: 'test4'
  }, ];

Thank you

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artur, 2016-03-22
@astralo

I found the solution myself, removed ng-options 'track by type.id' at the end

B
bromzh, 2016-03-23
@bromzh

Very clumsy decision. The select is bound to the 'select' field, here in the controller and set the desired initial value:

$scope.types = [{
    id: 1,
    name: 'test1'
  }, {
    id: 2,
    name: 'test2'
  }, {
    id: 3,
    name: 'test3'
  }, {
    id: 4,
    name: 'test4'
  }, ];
$scope.select = $scope.types[1];

Well, it would be worth looking into .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question