AngularJS: 使用ng-option產生下拉框,添加全部選擇項,angularjsng-option

來源:互聯網
上載者:User

AngularJS: 使用ng-option產生下拉框,添加全部選擇項,angularjsng-option

如下:


HTML代碼:


 <div class="controls  pull-left"> 消費類型:      <select id="selectType" ng-model="currentType"  ng-options="optiontype.id as optiontype.typeName for optiontype in typemodel">      </select> </div>


實現方法:

1、通過Angular JS的$http請求,動態擷取下拉框的選項。

2、使用unshift函數添加全部選項

    self.getConsumTypeList = function () {        return $http.get("/consumptiontype/listall").success(function (response) {            $scope.typemodel = response.data;            $scope.typemodel.unshift({id:null,typeName:'全部'});        }).error(function (response) {            $log.debug("請求逾時或網路故障!獲得列表失敗!")        });    };

unshift函數介紹:

unshift() 方法可向數組的開頭添加一個或更多元素,並返回新的長度。


文法:
arrayObject.unshift(newelement1,newelement2,....,newelementX)
unshift() 方法將把它的參數插入 arrayObject 的頭部,並將已經存在的元素順次地移到較高的下標處,以便留出空間。

請注意,unshift() 方法不建立新的建立,而是直接修改原有的數組。

注釋:unshift() 方法無法在 Internet Explorer 中正確地工作!

提示:要把一個或多個元素添加到數組的尾部,請使用 push() 方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.