AngularJS實(shí)現(xiàn)動態(tài)添加Option的方法
本文實(shí)例講述了AngularJS實(shí)現(xiàn)動態(tài)添加Option的方法。分享給大家供大家參考,具體如下:
項(xiàng)目中后臺管理設(shè)置,前臺下拉動態(tài)添加option
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="bootstrap.min.css" rel="external nofollow" > <script src="jQuery.min.js"></script> <script src="angular.js"></script> <script src="angular-animate.js"></script> <script src="bootstrap.min.js"></script> <script type="text/JavaScript"> var app = angular.module('myapp', []); app.controller('DemoCtrl', function ($scope) { $scope.optionContainer = []; var realOptions = []; var randomCode = function() { var chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ1234567890"; var randomChars = ""; for (var i = 0; i < 10; i++) { var index = Math.floor(Math.random() * chars.length); randomChars = randomChars + chars.charAt(i); } return randomChars; } var getIndex = function(array, id) { var tmpItem = {}; angular.forEach(array, function(item) { if (item.id == id) { tmpItem = item; } }); return array.indexOf(tmpItem); } $scope.add = function() { var optionIndex = randomCode(); $scope.optionContainer.push({ id : optionIndex, readOnly : false, content : '', showConfirm : true }) console.log($scope.optionContainer) } $scope.confirm = function(content, id) { if (content == '') { return; } var flag = false; angular.forEach(realOptions, function(item) { if (item == content) { flag = true; } }); if (flag) { console.log('already exist!'); return; } var tmpIdIndex = getIndex($scope.optionContainer, id); realOptions.push(content); $scope.optionContainer[tmpIdIndex].showConfirm = false; $scope.optionContainer[tmpIdIndex].readOnly = true; } $scope.deleteFunc = function(id) { var tmpIdIndex = getIndex($scope.optionContainer, id); if ($scope.optionContainer[tmpIdIndex].showConfirm == false) { tmpIndex = realOptions.indexOf($scope.optionContainer[tmpIdIndex].content); realOptions.splice(tmpIndex, 1); } $scope.optionContainer.splice(tmpIdIndex, 1); } }); </script> </head> <body ng-app="myapp"> <div ng-controller="DemoCtrl"> <div> <div class="Container"> <h1>create options</h1> </div> <div> <div> <table class="table table-striped table-hover"> <thead> <tr> <th>option</th> </tr> </thead> <tbody> <tr ng-repeat="item in optionContainer" class="row"> <td class="col-md-8" style="width:100%;"> <input type="text" ng-model="item.content" ng-readonly="item.readOnly"/></td> <td class="col-md-2"> <button type="button" class="btn btn-success btn-xs" ng-click="confirm(item.content, item.id)" ng-show="item.showConfirm">Confirm </button> </td> <td class="col-md-2"> <button type="button" class="btn btn-success btn-xs" ng-click="deleteFunc(item.id)">Delete </button> </td> </tr> </tbody> </table> </div> <a class="btn btn-success btn-xs" ng-click="add()">Add</a> </div> </div> </div> </body> </html>
運(yùn)行效果圖如下:
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對大家AngularJS程序設(shè)計(jì)有所幫助。
- AngularJS動態(tài)菜單操作指令
- angularjs+bootstrap菜單的使用示例代碼
- AngularJS實(shí)現(xiàn)樹形結(jié)構(gòu)(ztree)菜單示例代碼
- 實(shí)例詳解AngularJS實(shí)現(xiàn)無限級聯(lián)動菜單
- AngularJS自定義指令之復(fù)制指令實(shí)現(xiàn)方法
- AngularJS使用ng-class動態(tài)增減class樣式的方法示例
- AngularJS全局警告框?qū)崿F(xiàn)方法示例
- AngularJS+bootstrap實(shí)現(xiàn)動態(tài)選擇商品功能示例
- AngularJS自定義指令實(shí)現(xiàn)面包屑功能完整實(shí)例
- AngularJS使用攔截器實(shí)現(xiàn)的loading功能完整實(shí)例
- AngularJS折疊菜單實(shí)現(xiàn)方法示例
相關(guān)文章
angularjs實(shí)現(xiàn)天氣預(yù)報功能
這篇文章主要為大家詳細(xì)介紹了angularjs實(shí)現(xiàn)天氣預(yù)報功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10詳解從angular-cli:1.0.0-beta.28.3升級到@angular/cli:1.0.0
本篇文章主要介紹了詳解從angular-cli:1.0.0-beta.28.3升級到@angular/cli:1.0.0,具有一定的參考價值,有興趣的可以了解一下2017-05-05Angular5給組件本身的標(biāo)簽添加樣式class的方法
本篇文章主要介紹了Angular 5 給組件本身的標(biāo)簽添加樣式class的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04AngularJS自定義服務(wù)與fliter的混合使用
這篇文章主要介紹了AngularJS自定義服務(wù)與fliter的混合使用的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11Angular學(xué)習(xí)筆記之集成三方UI框架、控件的示例
這篇文章主要介紹了Angular學(xué)習(xí)筆記之集成三方UI框架、控件的示例,詳細(xì)的介紹了Material UI、Ag-grid等框架,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03Angular.JS實(shí)現(xiàn)無限級的聯(lián)動菜單(使用demo)
這篇文章主要介紹了Angular.JS中實(shí)現(xiàn)無限級聯(lián)動菜單的使用示例,本文是在之前的一篇文章的基礎(chǔ)上進(jìn)行的幾個demo分享,有需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02