How remove first empty element in dropdown in AngularJS1.4.3

Hello, currently I have some count of legacy projects on the support and they are has been created with AngularJS 1.4.3. 


This is framework has features what first element element in dropdown will be empty.

By design it's must be element with label as 'Select from dropdown'.


Problem:



The original code:

<select ng-model="feed.config">
    <option ng-repeat="template in configs" ng-selected="$first">{{template.name}}</option>
</select>

Because this is versions is intermediate, solutions as 

<option value="" style="display: none"></option>

or

<option value="" ng-if = "false"></option>

or

<option value="" ng-show = "false"></option>

Some cituation with all solutions from this is link:

https://ask-dev.ru/info/13515/why-does-angularjs-include-an-empty-option-in-select

https://jsfiddle.net/MTfRD/3/

https://stackoverflow.com/questions/44639911/how-to-remove-an-empty-option-after-a-filter-modifies-the-drop-down-list-in-angu

https://syntaxfix.com/question/11449/remove-blank-option-from-select-option-with-angularjs


Not will be work. Modificated code:

Worked solution for this is version:

<select ng-model="model.id" convert-to-number>
  <option value="0">Zero</option>
  <option value="1">One</option>
  <option value="2">Two</option>
</select>
{{ model }}


angular.module('nonStringSelect', [])
.run(function($rootScope) {
  $rootScope.model = { id: 2 };
})
.directive('convertToNumber', function() {
  return {
    require: 'ngModel',
    link: function(scope, element, attrs, ngModel) {
      ngModel.$parsers.push(function(val) {
        return parseInt(val, 10);
      });
      ngModel.$formatters.push(function(val) {
        return '' + val;
      });
    }
  };
});





References:

ngSelected : https://docs.angularjs.org/api/ng/directive/ngSelected

$first : https://docs.angularjs.org/api/ng/directive/ngRepeat

Examples with some problem: https://syntaxfix.com/question/11449/remove-blank-option-from-select-option-with-angularjs

Docs for version:  https://code.angularjs.org/1.4.3/docs/api/ng/directive/select

Комментарии

Популярные сообщения из этого блога

Вопросы с собеседования С++ (2018)