简体   繁体   中英

ng-selected with ng-repeat in select

Sorry for asking again . But I cant set default value for select . Here is code I used

Plnkr

I cant use ng-options in this case . Please help me without ng-options

If the model has the value same as option defined, ng-selected would work as:

ng-selected="business_entity == businessConstants"

given the model is such :

$scope.formData = {business_entity : 'Công ty TNHH'};

Forked Plunker

Try to use the code in your controller as below,

$scope.formData={};
$scope.formData.business_entity = 'Công ty TNHH';

And please remove 'ng-selected' from your option tag from your tempalte.

<select ng-model="formData.business_entity">
  <option ng-repeat="businessConstants in businessConstants" value="{{businessConstants}}">
    {{businessConstants}}
  </option>
</select>

I've update your plnk please check..

remove ng-select and assign array reference to ng-model variable

$scope.formData = {
     'business_entity' : $scope.businessConstants[1]
}

Html

<select name="" id="" 
    ng-model="formData.business_entity">
      <option  
      ng-repeat="businessConstants in businessConstants" 
      value="{{businessConstants}}">
                                {{businessConstants}}
                                    </option>
    </select>

Demo

Change your HTML to

<body ng-controller="ctrl">
    <select name="" id="" 
        ng-options="option for option in businessConstants"
        ng-model="business_entity">
    </select>
</body>

Your options will be automatically generated and the select will be bound to business_entity automatically

Change Controller to

function ctrl($scope) { 

  $scope.businessConstants = [
    'Công ty Cổ phần',
    'Công ty TNHH',
    'Tổ chức nhà nước',
    'Ngân hàng',
    'Trường học',
    'Cá nhân',
    'Khác'
  ];

  $scope.business_entity = $scope.businessConstants[4];
}

This will set the default value. Make sure you select the default value from the existing collection businessConstants and not assign it.

You can refer updated Plunker link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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