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自定義指令之復(fù)制指令實現(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)文章
詳解從angular-cli:1.0.0-beta.28.3升級到@angular/cli:1.0.0
本篇文章主要介紹了詳解從angular-cli:1.0.0-beta.28.3升級到@angular/cli:1.0.0,具有一定的參考價值,有興趣的可以了解一下2017-05-05
AngularJS自定義服務(wù)與fliter的混合使用
這篇文章主要介紹了AngularJS自定義服務(wù)與fliter的混合使用的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11
Angular.JS實現(xiàn)無限級的聯(lián)動菜單(使用demo)
這篇文章主要介紹了Angular.JS中實現(xiàn)無限級聯(lián)動菜單的使用示例,本文是在之前的一篇文章的基礎(chǔ)上進行的幾個demo分享,有需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02

