Как добавить option в Angular?
Как добавить новый option в уже существующий select list в разметке? Сейчас первый пункт option пустой
Источник: Stack Overflow на русском
Как добавить новый option в уже существующий select list в разметке? Сейчас первый пункт option пустой
ng-options
HTML:
$scope.options = [{key:"1", value: "value1"},{key:"2", value: "value2"},{key:"3", value: "value3"}];
Angular:
<select ng-options="opt.key for opt in options "></select>
directive
:(function() {
var app = angular.module("myapp", []);
app.directive("drSelect", function() {
return {
restrict: "A",
compile: function(element, attributes) {
return {
pre: function(scope, element, attributes, controller, transcludeFn) {
var newDirective = angular.element('<option>123</option');
element.append(newDirective);
}
}
}
};
});
}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<div ng-app="myapp">
<select dr-select></select>
</div>
Ссылка на пример