当前位置: 首页 » 产品 » 微商货源 » 正文

AngularJS如何实现动态添加Option

放大字体  缩小字体 发布日期: 2024-11-26 08:42   来源:http://www.baidu.com/  作者:无忧资讯  浏览次数:24
核心提示:项目中后台管理设置,前台下拉动态添加option!DOCTYPE htmlhtml lang=zh-CN headmeta charset=utf-8meta http-equiv=X-UA-Compat

项目中后台管理设置,前台下拉动态添加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=http://www.3lian.com/edu/2017/05-17/"bootstrap.min.css" rel="external nofollow" > <script src=http://www.3lian.com/edu/2017/05-17/"jQuery.min.js"></script> <script src=http://www.3lian.com/edu/2017/05-17/"angular.js"></script> <script src=http://www.3lian.com/edu/2017/05-17/"angular-animate.js"></script> <script src=http://www.3lian.com/edu/2017/05-17/"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.show/confirm/i">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>

运行效果图如下:

AngularJS如何实现动态添加Option 三联

 
 
[ 产品搜索 ]  [ 加入收藏 ]  [ 告诉好友 ]  [ 打印本文 ]  [ 违规举报 ]  [ 关闭窗口 ]

 

 
推荐图文
推荐产品
点击排行
    行业协会  备案信息  可信网站