0

Controller

$scope.industries= [
 {
  id: 1,
  name: "Name1", 
 }, {
  id: 2,
  name: "Name2"
 }
];

When assign $scope.filling_type = 2 in controller,I want to select "Name2" option in the select.

View

<select ng-model="filling_type">
  <option ng-repeat="industry in industries"
  ng-value="industry.name">{{industry.name}}</option>
</select>

In this case no option selected.

2
  • Isn't it for the capital "I" in {{Industry.name}} ? Commented Sep 4, 2018 at 12:08
  • made that mistake when i post this. Commented Sep 4, 2018 at 12:11

2 Answers 2

2

I'd recommend getting used to ng-options in the select element instead of ng-repeating in the option element because of increased flexibility and performance. https://docs.angularjs.org/api/ng/directive/select#choosing-between-ngrepeat-and-ngoptions-

var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
$scope.industries= [
 {
  id: 1,
  name: "Name1", 
 }, {
  id: 2,
  name: "Name2"
 }
];

$scope.filling_type = 2; // set default option here, using 2 from OP
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">
<select 
    ng-model="filling_type"
    ng-options="i.id as i.name for i in industries"> 
</select>
<p>Filling Type: {{filling_type}}</p>
</body>

Sign up to request clarification or add additional context in comments.

Comments

1

Since you are binding name as value, you should set the name property as the filling_type

DEMO

var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
$scope.industries= [
 {
  id: 1,
  name: "Name1", 
 }, {
  id: 2,
  name: "Name2"
 }
];

$scope.filling_type = "Name2";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">
<select ng-model="filling_type">
  <option ng-repeat="industry in industries"
  ng-value="industry.name">{{industry.name}}</option>
</select>
</body>

1 Comment

ng-value="industry.name" is needed now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.