• SQL Tutorial
  • PHP
  • RubyonRails
  • Struts
  • jQuery
  • Servlet
  • JSP
  • Spring
  • Java IO

  • <

    AngularJS Select

    AngularJS lets you create dropdown lists based on items in an array, or an object.



    Using ng-options



    
    
    <!DOCTYPE html>  
    <html>  
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
    <body>  
    <div ng-app="myApp" ng-controller="myCtrl">  
    <select ng-model="selectedName" ng-options="x for x in names">  
    </select>  
    </div>  
    <script>  
    var app = angular.module('myApp', []);  
    app.controller('myCtrl', function($scope) {  
        $scope.names = ["Java", "PHP", ".Net", "AngularJS", "C/C++"];  
    });  
    </script>  
    </body>  
    </html>  
    

    You can also use the ng-repeat directive to make the same dropdown list as ng-options.


     <!DOCTYPE html>  
    <html>  
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
    <body>  
    <div ng-app="myApp" ng-controller="myCtrl">  
    <select>  
    <option ng-repeat="x in names">{{x}}</option>  
    </select>  
    </div>  
    <script>  
    var app = angular.module('myApp', []);  
    app.controller('myCtrl', function($scope) {  
            $scope.names = ["Java", "PHP", ".Net", "AngularJS", "C/C++"];  
    });  
    </script>  
    <p>The same example of dropdown list using the ng-repeat directive.</p>  
    </body>  
    </html>  
    
    
    


    ng-options vs. ng-repeat



    Although, both can be used for dropdown list, but ng-repeat directive repeats a block of HTML code for each item in an array, it can be used to create options in a dropdown list, while the ng-options directive was made especially for filling a dropdown list with options, and has at least one important advantage:

    Dropdowns made with ng-options allows the selected value to be an object, while dropdowns made from ng-repeat has to be a string.

    
    $scope.cars = [  
        {model : "Ford Mustang", color : "red"},  
        {model : "Fiat 500", color : "white"},  
        {model : "Volvo XC90", color : "black"}  
    ];  
    
    


    Limitation of ng-repeat



    The ng-repeat directive has a limitation that the selected value must be a string:

    <!DOCTYPE html>  
    <html>  
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
    <body>  
    <div ng-app="myApp" ng-controller="myCtrl">  
    <p>Select a car:</p>  
    <select ng-model="selectedCar">  
    <option ng-repeat="x in cars" value="{{x.model}}">{{x.model}}</option>  
    </select>  
    <h1>You selected: {{selectedCar}}</h1>  
    </div>  
    <script>  
    var app = angular.module('myApp', []);  
    app.controller('myCtrl', function($scope) {  
        $scope.cars = [  
            {model : "Ford", color : "red"},  
            {model : "Honda", color : "white"},  
            {model : "Volvo", color : "black"},  
            {model : "Hundai", color : "gray"}  
        ];  
    });  
    </script>  
    </body>  
    </html>  
    
    
    

    While using the ng-options directive, you can select an object value:


    
    <!DOCTYPE html>  
    <html>  
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
    <body>  
    <div ng-app="myApp" ng-controller="myCtrl">  
    <p>Select a car:</p>  
    <select ng-model="selectedCar" ng-options="x.model for x in cars">  
    </select>  
    <h1>You selected: {{selectedCar.model}}</h1>  
    <p>It's color is: {{selectedCar.color}}</p>  
    </div>  
    <script>  
    var app = angular.module('myApp', []);  
    app.controller('myCtrl', function($scope) {  
           $scope.cars = [  
            {model : "Ford", color : "red"},  
            {model : "Honda", color : "white"},  
            {model : "Volvo", color : "black"},  
            {model : "Hundai", color : "gray"}  
        ];  
      
    });  
    </script>  
    </body>  
    </html>  
    
    


    Use data source as an object



    
    $scope.cars = {  
            car01 : "Ford",  
            car02 : "Honda",  
            car03 : "Volvo",  
            car03 : "Hundai",  
        };  
    
    

    The expression in the ng-options attribute is a bit different for objects:


    <!DOCTYPE html>  
    <html>  
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
    <body>  
    <div ng-app="myApp" ng-controller="myCtrl">  
    <p>Select a car:</p>  
    <select ng-model="selectedCar" ng-options="x for (x, y) in cars">  
    </select>  
    <h1>You selected: {{selectedCar}}</h1>  
    </div>  
    <script>  
    var app = angular.module('myApp', []);  
    app.controller('myCtrl', function($scope) {  
        $scope.cars = {  
            car01 : "Ford",  
            car02 : "Honda",  
            car03 : "Volvo",  
            car03 : "Hundai",  
        }  
    });  
    </script>  
    </body>  
    </html>  
    
    
    

    The selected value will always be the value in a key-value pair.

    The value in a key-value pair can also be an object:


    $scope.cars = {  
    car01 : {brand : "Ford", model : "Mustang", color : "red"},  
    car02 : {brand : "Honda", model : "city", color : "white"},  
    car03 : {brand : "Volvo", model : "XC90", color : "black"},  
    car04 : {brand : "Hundai", model : "Creta", color : "gray"}  
    };  
    
    
    


    Example



    
    <!DOCTYPE html>  
    <html>  
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
    <body>  
    <div ng-app="myApp" ng-controller="myCtrl">  
    <p>Select a car:</p>  
    <select ng-model="selectedCar" ng-options="x for (x, y) in cars">  
    </select>  
    <h1>You selected: {{selectedCar.brand}}</h1>  
    <h2>Model: {{selectedCar.model}}</h2>  
    <h3>Color: {{selectedCar.color}}</h3>  
    </div>  
    <script>  
    var app = angular.module('myApp', []);  
    app.controller('myCtrl', function($scope) {  
        $scope.cars = {  
    car01 : {brand : "Ford", model : "Mustang", color : "red"},  
    car02 : {brand : "Honda", model : "city", color : "white"},  
    car03 : {brand : "Volvo", model : "XC90", color : "black"},  
    car04 : {brand : "Hundai", model : "Creta", color : "gray"}  
        }  
    });  
    </script>  
    </body>  
    </html>  
    
    
















    © copyright 2017-2021 Completedone pvt ltd.