时间:2021-05-28
本文实例讲述了AngularJS实现动态添加Option的方法。分享给大家供大家参考,具体如下:
项目中后台管理设置,前台下拉动态添加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>运行效果图如下:
更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》
希望本文所述对大家AngularJS程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了AngularJS实现动态编译添加到dom中的方法。分享给大家供大家参考,具体如下:在使用angularjs时,希望通过动态构建angular模版
今天有朋友问我一个关于在里动态添加option问题,一开始以为是JS那里动态添加,所以用了JS动态添加option的方法,但你那里是用JQuery的,所以才会一
1、基础准备: 先来了解下,如何运用js实现select动态添加option。//1.动态创建selectfunctioncreateSelect(){var
jquery动态添加optionjs动态添加option复制代码代码如下:varsel=document.getElementById("Selected1")
antdSelect下拉菜单动态添加option里的内容,通过form表单绑定select选中的值提供一个公共的方法,每次只需去调用这个方法就行了//这里是示例