0

ng-repeat creates empty object, when using with <select> and <option>. However, the array looks normal. Every element is defined, no empty elements.

I've tried change places, but I don't understand why it works so.

<select ng-model="searchCategories">
   <option ng-repeat="c in recordings" value="[[ c.title ]]">[[ c.title ]]></option>
</select>

ng-repeat produces empty object, like this: <option value="? object:125 ?"></option>

3
  • is interpolation done like [[c.title]] this by mistake? because it's supposed to be like this {{c.title}} Commented Apr 1, 2019 at 14:19
  • By the way, ng-options is the better choice for select than ng-repeat. docs.angularjs.org/api/ng/directive/ngOptions Commented Apr 1, 2019 at 14:22
  • Which version of angularjs ? I assume you changed '{{' to '[[' via $interpolateProvider. You should use ng-value="c.title". There were lot of bugs with angularjs select/options handlers. In angularjs 1.7 you should be safe, try to upgrade if it isn't . ng-options is NOT REQUIRED to make that work. Commented Apr 1, 2019 at 14:31

1 Answer 1

1

I have made a search and found this on the API Select documentation of AngularJS:

Choosing between ngRepeat and ngOptions
In many cases, ngRepeat can be used on elements instead of ngOptions to achieve a similar result. However, ngOptions provides some benefits:
More flexibility in how the <select> model is assigned via the select as part of the comprehension expression reduced memory consumption by not creating a new scope for each repeated instance increased render speed by creating the options in a documentFragment instead of individually Specifically, select with repeated options slows down significantly starting at 2000 options in Chrome and Internet Explorer / Edge.

So it's much better to use ngOptions instead of ngRepeat
Based on your problem, we need to specify a default value to avoid that it generates an empty object.
And as Pierre Emmanuel Lalleman says in the latest 1.7.x version of Angular, some bugs based on this problem are corrected. I'm gonna set the 1.4.8 AngularJS version, but when you upgrade to the last version you will notice immediately the difference.

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

  $scope.searchCategories;
  $scope.recordings = [
  	{title: 'Medical'},
    {title: 'Engineering'},
    {title: 'Marketing'},
    {title: 'IT'},
    {title: 'Videogames'},
  ];
  $scope.setValue = function(){
  	$scope.searchCategories = $scope.recordings[0];
  };
  //With more information
  $scope.data = {
    availableOptions: [
      {id: '1', name: 'Medical'},
      {id: '2', name: 'Engineering'},
      {id: '3', name: 'Marketing'}
    ],
    selectedOption: {id: '3', name: 'Marketing'} //This sets the default value of the select in the ui
    };
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
<!-- Change to this version -->
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>-->

<h1>Select with AngularJS</h1>
<div ng-app="myApp" ng-controller="myCtrl">
<h5>Selected option: <label>{{searchCategories}}</label></h5>
<label>
  With ng-repeat
</label>
<select ng-model="searchCategories">
   <option ng-repeat="c in recordings" ng-value="{{c.title}}">{{c.title}}</option>
</select>
<br>
<label>
  With ng-option
</label>
<select ng-model="searchCategories" ng-options="c as c.title for c in recordings">
</select>
<button ng-click="setValue()">
Give value by default
</button>
<br>
<label>
  With ng-options, data-id and default selected option
</label>
<select
      ng-options="option.name for option in data.availableOptions track by option.id"
      ng-model="data.selectedOption"></select>
      <br>
      {{data.selectedOption}}
</div>

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

1 Comment

Thank you for good answer! I have tried all your variations, but at the end I found out that there was indeed empty object inserted into the array of values.. I'm very inattentive

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.