2017-02-22 7 views
-1

ich als eine Mehrfachauswahl Dropdown habe unterwie Standardwert in mehrere Auswahl Dropdown-Liste Winkel js setzen

<select id="year" multiple ng-model="type" ng-disabled="!type1" > 
    <option value="all" selected>all</option> 
    <option value='2012'>2012</option> 
    <option value='2013'>2013</option> 
    <option value='2014'>2014</option> 
    <option value='2015'>2015</option> 
    <option value='2016'>2016</option> 
    <option value='2017'>2017</option> 
    <option value='2018'>2018</option> 
    <option value='2019'>2019</option> 
</select> 

Ich mag Option „All“ als Standardwert eingestellt werden. Dafür habe ich das Schlüsselwort "selected" innerhalb des Options-Tags geschrieben, das standardmäßig in meiner Dropdown-Liste angezeigt werden soll, wie Sie im obigen Code sehen können. Aber es funktioniert nicht. Kann mir bitte jemand helfen wie kann ich den Standardwert einstellen.

+1

Werfen Sie einen Blick auf ngOptions https://docs.angularjs.org/api/ng/directive/ngOptions die Winkel Art und Weise zu verwenden. – fubbe

+0

Können Sie Ihren Controllercode auch posten? –

+0

Könnten Sie überprüfen, ob [meine Antwort] (http://stackoverflow.com/a/42396482/4927984) Ihr Problem gelöst hat? – Mistalis

Antwort

1

Sie können den Standard in Ihrem Controller festlegen. Denken Sie daran, dass der Wert eines Multiselects ein Array ist, also müssen Sie $scope.type auf ein Array setzen. Hier ist ein funktionierendes Beispiel:

angular.module('app', []) 
 
    .controller('ctrl', function($scope) { 
 
    $scope.type = ["all"]; 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
    <select id="year" multiple ng-model="type"> 
 
     <option value="all" selected>all</option> 
 
     <option value='2012'>2012</option> 
 
     <option value='2013'>2013</option> 
 
     <option value='2014'>2014</option> 
 
     <option value='2015'>2015</option> 
 
     <option value='2016'>2016</option> 
 
     <option value='2017'>2017</option> 
 
     <option value='2018'>2018</option> 
 
     <option value='2019'>2019</option> 
 
    </select> 
 
    <div> 
 
    {{type}} 
 
    </div> 
 
</div>

0

Der Angular Weg wäre zu verwenden ng-options:

<select ng-model="type" ng-options="o for o in options"/> 

Wo Optionen wird ein Array in Ihrem Controller eingestellt:

$scope.options = [2012, 2013, 2014, 2015, 2016, ...]; 

var myApp = angular.module('myApp', []); 
 

 
function MyCtrl($scope) { 
 
    $scope.options = [2011, 2012, 2013, 2014]; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="MyCtrl"> 
 
    <select ng-options="o for o in options" ng-model="type"> 
 
    <option value="">all</option> 
 
    </select> 
 
    <br>selected: {{type}} 
 
</div>

Verwandte Themen