AngularJS怎么实现动态添加Option
短信预约 -IT技能 免费直播动态提醒
这篇文章主要为大家展示了“AngularJS怎么实现动态添加Option”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“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 class="lazy" data-src="jQuery.min.js"></script>
<script class="lazy" data-src="angular.js"></script>
<script class="lazy" data-src="angular-animate.js"></script>
<script class="lazy" data-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">
<h2>create options</h2>
</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" >
<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怎么实现动态添加Option”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341