AngularJS實現(xiàn)動態(tài)添加Option的方法
本文實例講述了AngularJS實現(xiàn)動態(tài)添加Option的方法。分享給大家供大家參考,具體如下:
項目中后臺管理設(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>
運行效果圖如下:

更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對大家AngularJS程序設(shè)計有所幫助。
- AngularJS動態(tài)菜單操作指令
- angularjs+bootstrap菜單的使用示例代碼
- AngularJS實現(xiàn)樹形結(jié)構(gòu)(ztree)菜單示例代碼
- 實例詳解AngularJS實現(xiàn)無限級聯(lián)動菜單
- AngularJS自定義指令之復制指令實現(xiàn)方法
- AngularJS使用ng-class動態(tài)增減class樣式的方法示例
- AngularJS全局警告框?qū)崿F(xiàn)方法示例
- AngularJS+bootstrap實現(xiàn)動態(tài)選擇商品功能示例
- AngularJS自定義指令實現(xiàn)面包屑功能完整實例
- AngularJS使用攔截器實現(xiàn)的loading功能完整實例
- AngularJS折疊菜單實現(xiàn)方法示例
相關(guān)文章
理解AngularJs篇:30分鐘快速掌握AngularJs
這篇文章主要介紹了理解AngularJs篇:30分鐘快速掌握AngularJs,詳細介紹了AngularJs所涉及的知識點,有興趣的可以了解一下。2016-12-12
AngularJS constant和value區(qū)別詳解
angularJS可以通過constant(name,value)和value(name,value)對于創(chuàng)建服務(wù)也是很重要的。他們之間有什么不同呢?今天小編給大家分享AngularJS constant和value區(qū)別詳解,需要的朋友參考下2017-02-02
AngularJS使用ocLazyLoad實現(xiàn)js延遲加載
這篇文章主要介紹了AngularJS使用ocLazyLoad實現(xiàn)js延遲加載的相關(guān)資料,需要的朋友可以參考下2017-07-07
AngularJS 基礎(chǔ)ng-class-even指令用法
本文主要介紹AngularJS ng-class-even 指令,這里整理了ng-class-even基礎(chǔ)知識資料,并附實例代碼和效果圖,學習AngularJS指令的朋友可以看下2016-08-08
AngularJS基礎(chǔ) ng-class-odd 指令示例
本文主要介紹AngularJS ng-class-odd 指令,這里對ng-class-odd基礎(chǔ)知識做了詳細整理,并有示例代碼和效果圖,學習AngularJS的同學可以參考下2016-08-08
AngularJS動態(tài)添加數(shù)據(jù)并刪除的實例
下面小編就為大家分享一篇AngularJS動態(tài)添加數(shù)據(jù)并刪除的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
angular route中使用resolve在uglify壓縮后問題解決
這篇文章主要介紹了angular route中使用resolve在uglify壓縮后問題解決的相關(guān)資料,需要的朋友可以參考下2016-09-09

