天天看點

angularJS的select(選擇框)

以下代碼可直接複制儲存為html文檔檢視效果

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>AngularJS的select(選擇框)</title>
        <script src="js/Angular.js"></script>
    </head>
    <body>
        <div ng-app="selectApp" ng-controller="selectController">
            <div>
                <h4>使用ng-option建立項</h4>
                <select ng-init="selectedName = names[0]" ng-model="selectedName" ng-options="x for x in names">
                </select>

                <h4>使用repeat建立項</h4>
                <select>
                    <option ng-repeat="x in names">{{x}}</option>
                </select>
            </div>
            <div>
                <h4>建立對象項</h4>
                <div>
                    <h5>使用repeat建立:獲得的選中項是一串字元串</h5>
                    <select ng-model="selectedSite">
                        <option ng-repeat="x in sites" value="{{x.url}}">{{x.site}}</option>
                    </select>
                    <p>你選擇的是: {{selectedSite}}</p>
                </div>
                <div>
                    <h5>使用ng-option建立:獲得的選中項是一個對象</h5>
                    <select ng-model="selectedSite" ng-options="x.site for x in sites"></select>

                    <p>你選擇的是: {{selectedSite.site}}</p>
                    <p>網址為: {{selectedSite.url}}</p>
                </div>
            </div>
            <div>
                <h4>建立資料源項</h4>
                <p style="color: red;">使用對象作為資料源, x 為鍵(key), y 為值(value):</p>
                <select ng-model="dataOption" ng-options="x for (x, y) in data">
                </select>
                <h1>你選擇的值是: {{dataOption}}</h1>
                <p style="color: red;">選中的項的y值可以是字元串,也可以是對象,當y值為對象時也可使用y的一個屬性作為項顯示名稱<br/> 
                例:
                <select ng-model="selectedCar" ng-options="y.brand for (x, y) in cars">
                </select>
                <p>你選擇的是: {{selectedCar.brand}}</p>
                <p>型号為: {{selectedCar.model}}</p>
                <p>顔色為: {{selectedCar.color}}</p>

                </p>
            </div>
            <script>
                var app = angular.module("selectApp",[]);
                app.controller("selectController",function($scope){
                    $scope.names = ["Google", "Yoho", "Taobao"];

                    $scope.sites = [
                        {site : "Google", url : "http://www.google.com"},
                        {site : "baidu", url : "http://www.baidu.com"},
                        {site : "Taobao", url : "http://www.taobao.com"}
                    ];

                    $scope.data = {
                        site01 : "Google",
                        site02 : "baidu",
                        site03 : "Taobao"
                    };

                    $scope.cars = {
                        car01 : {brand : "Ford", model : "Mustang", color : "red"},
                        car02 : {brand : "Fiat", model : "500", color : "white"},
                        car03 : {brand : "Volvo", model : "XC90", color : "black"}
                    };
                })
            </script>
        </div>
        <div>
            <h3>選擇框</h3>
            在 AngularJS 中我們可以使用 ng-option 指令來建立一個下拉清單,清單項通過對象和數組循環輸出<br />
            我們也可以使用ng-repeat 指令來建立下拉清單:<br />
            差別:<br />
            ng-repeat 指令是通過數組來循環 HTML 代碼來建立下拉清單,但 ng-options 指令更适合建立下拉清單,它有以下優勢:<br />
            使用 ng-options 的選項的一個對象, ng-repeat 是一個字元串。<br />

        </div>
        <div>
            <h3></h3>
        </div>
        <div>
            <h3></h3>
        </div>
        <div>
            <h3></h3>
        </div>
    </body>
</html>