0
<select class="form-control" ng-model="companyId" style="height: 40px;" ng-options="id for id in companyId">
</select>

Hi.I have an array scope.companyId in controller which gets populated with the result of controller.array contains say [1,2,3].I need to populate this in dropdown.What is the mistake im doing?.

Thanks.

2
  • how looks your js code. is it possible to create a fiddle or plunker? Commented Mar 21, 2017 at 8:46
  • we would especially need to see your controller Commented Mar 21, 2017 at 8:49

4 Answers 4

1

Change ng-model to something else. Your array name and ng-model are same.

<select class="form-control" ng-model="selectedId" style="height: 40px;" ng-options="id for id in companyId">
</select>

Since your ng-model and array (companyId) are same. After selecting a number, companyId will become variable not an array.

var app = angular.module("app", []);
app.controller("ctrl", function($scope) {
$scope.companyId=[1,2,3,4];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <select class="form-control" ng-model="selectedId" style="height: 40px;" ng-options="id for id in companyId">
</select>
</div>

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

Comments

0

Try this:

<select class="form-control" ng-model="companyIdModel" style="height: 40px;" ng-options="company_id as id for id in companyId">

Comments

0

you can set any item which should be pre-selected as value of select's ngModel

you try like this,

<div ng-app="myApp" ng-controller="myCtrl">

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.companyId = [1,2,3];
});
</script>

Comments

0

Lets use this

<select ng-model="companyId" class="dropdown form-control input-sm"
                        ng-options="company as company for company in companyList"></select>

I suggest you to rename this $scope.companyId to $scope.companyList. or rename the binded model to your dropdown. Suggest you the following in js

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.companyList= [1,2,3];
})

Here is the plnkr for you. https://plnkr.co/edit/eNVKGeE0M92keNRDoQEE?p=preview

Comments

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.