Angular对标签的增删改查

<!DOCTYPE html>
<html>


<head>
<meta charset="utf-8" />
<title>第二周练习</title>
<link rel="stylesheet" href="css/sty.css" />
<script type="text/javascript" src="js/angular.min.js"></script>
</head>


<body ng-app="MyApp" ng-controller="MyCont">
<center>
<div class="div">
<span class="span">姓名查询条件</span><input class="ipt" type="text" ng-model="val" ng-click="aler()" ng-keydown="pan()" />
<select class="select" ng-model="countrys" ng-change="sele()" ng-init="countrys='按年龄正序'">
<option>按年龄正序</option>
<option>按年龄倒序</option>
</select>
</div>
<br />
<div ng-show="xs">
<span class="span2">用户列表</span><br />
<table class="table" cellpadding="0px" cellspacing="0px">
<tr style="background-color: gray;">
<td>姓名</td>
<td>年龄</td>
<td>拼音</td>
<td>职位</td>
<td>操作</td>
</tr>
<tr ng-repeat="li in xin">
<td>{{li.gname}}</td>
<td>{{li.gage}}</td>
<td>{{li.gpin}}</td>
<td>{{li.gzhi}}</td>
<td><input class="dl" type="button" value="删除" ng-click="del($index)" /></td>
</tr>
</table>
<input class="btn1" type="button" value="查询" ng-click="cha()" />
<input class="btn2" type="button" value="添加用户" ng-click="jump()" />
</div>
<div ng-show="xs2">
<form>
<fieldset>
<legend>添加用户信息</legend>
姓名<input type="text" ng-model="tname" /><br /> 年龄
<input type="text" ng-model="tage" ng-blur="nan()" /><br /> 拼音
<input type="text" ng-model="tpin" /><br /> 职业
<input type="text" ng-model="tzhi" /><br />
<input type="button" value="添加" ng-click="tian()" />
</fieldset>
</form>
</div>
</center>
<script>
var mo = angular.module("MyApp", []);
mo.controller("MyCont", function($scope) {
var jishu=0;
$scope.$watch('val', function(newValue, oldValue) {
if($scope.val != oldValue) {
var flag = /妈|爸|傻逼|滚蛋|草你妈|二逼|鸡巴/g;
if(flag.test($scope.val)){
alert("不能说脏话哦")
jishu++;
}
}
if(jishu!=0){
$scope.val = $scope.val.replace(/妈|爸|傻逼|滚蛋|草你妈|二逼|鸡巴/g,"***")
}
});
var arr3=[]
$scope.xs = true;
$scope.xin = [{
"gname": "李新阳",
"gage": "20",
"gpin": "lixinyang",
"gzhi": "创业青年"
}, {
"gname": "小胖",
"gage": "20",
"gpin": "liubinpeng",
"gzhi": "葬爱家族"
}, {
"gname": "张立朝",
"gage": "1000000",
"gpin": "zhanglichao",
"gzhi": "嘴强王者"
}, {
"gname": "老石",
"gage": "16",
"gpin": "shichenguang",
"gzhi": "小屁孩"
}]
arr3=$scope.xin;
$scope.sele = function() {
var val = $scope.countrys;
if(val == "按年龄正序") {
$scope.xin.sort(function(a, b) {
return a.gage - b.gage;
})
} else {
$scope.xin.sort(function(a, b) {
return b.gage - a.gage;
})
}
}
$scope.del = function($index) {
if(window.confirm("确定要删除么")) {
$scope.xin.splice($index, 1);
}
}
$scope.cha = function() {
//if(arr2.length!=0){
// $scope.xin = arr2
//}else{
$scope.xin=arr3
//}

var val = $scope.val;
var arr = []
if(val == undefined) {
alert("请输入后查找!谢谢")
return;
}
for(var i = 0; i < $scope.xin.length; i++) {
if(val == $scope.xin[i].gname) {
arr.push($scope.xin[i])
}
}
if(arr.length == 0) {
alert("不好意思没有查到信息")
return;
}
$scope.xin = arr;
}
$scope.aler = function() {
var val = $scope.val;
if(val == undefined || val == "") {
alert("请输入姓名")
}
}


$scope.jump = function() {
$scope.xs = false;
$scope.xs2 = true;
}
$scope.tian = function() {
var panse = {
"gname": $scope.tname,
"gage": $scope.tage,
"gpin": $scope.tpin,
"gzhi": $scope.tzhi
}
$scope.xin.push(panse)
if(arr3!=undefined){
arr3.push(panse)
}

$scope.xs = true;
$scope.xs2 = false;
}
$scope.nan = function() {
if(isNaN($scope.tage)) {
alert("请输入数字")
}
}
})
</script>
</body>


</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用AngularJS实现增删改查的示例代码: HTML模板: ```html <div ng-app="myApp" ng-controller="myCtrl"> <h2>用户列表</h2> <table> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>操作</th> </tr> </thead> <tbody> <tr ng-repeat="user in users"> <td>{{user.name}}</td> <td>{{user.age}}</td> <td> <button ng-click="editUser(user)">编辑</button> <button ng-click="deleteUser(user)">删除</button> </td> </tr> </tbody> </table> <h2>添加/编辑用户</h2> <form ng-submit="saveUser()"> <label>姓名:</label> <input type="text" ng-model="currentUser.name" required /> <br /> <label>年龄:</label> <input type="number" ng-model="currentUser.age" required /> <br /> <button type="submit">保存</button> </form> </div> ``` AngularJS控制器: ```javascript var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.users = [ { name: '张三', age: 20 }, { name: '李四', age: 25 }, { name: '王五', age: 30 } ]; $scope.currentUser = {}; $scope.editUser = function(user) { $scope.currentUser = angular.copy(user); }; $scope.saveUser = function() { if ($scope.currentUser.name && $scope.currentUser.age) { if ($scope.currentUser.hasOwnProperty('$index')) { $scope.users[$scope.currentUser.$index] = angular.copy($scope.currentUser); } else { $scope.users.push(angular.copy($scope.currentUser)); } $scope.currentUser = {}; } }; $scope.deleteUser = function(user) { var index = $scope.users.indexOf(user); if (index > -1) { $scope.users.splice(index, 1); } }; }); ``` 上述代码实现了一个简单的用户列表,包含姓名和年龄字段。用户可以添加、编辑和删除用户信息。使用ng-repeat指令在表格中显示用户列表,通过ng-click指令调用相应的方法进行编辑和删除操作,ng-model指令绑定用户输入的数据。保存时,根据当前用户是否包含索引属性来判断是新增还是编辑操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值