国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

AngularJS實(shí)現(xiàn)動態(tài)添加Option的方法

2019-11-19 16:34:17
字體:
供稿:網(wǎng)友

本文實(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ì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 龙陵县| 灯塔市| 凌云县| 宁河县| 钟山县| 新营市| 琼结县| 金昌市| 新安县| 宾川县| 崇信县| 安徽省| 永仁县| 寿阳县| 江山市| 浦北县| 南充市| 通河县| 房山区| 珲春市| 青海省| 南溪县| 万载县| 阳高县| 松潘县| 嘉定区| 仲巴县| 杭州市| 阿巴嘎旗| 双桥区| 微博| 延寿县| 朝阳县| 淮阳县| 乐业县| 昆山市| 西华县| 会东县| 吉隆县| 乌恰县| 莒南县|