Angular 前后端分离

1. 项目结构

 index.html

<!DOCTYPE html>
<html lang="en"  ng-app="myApp">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
//顺序不要变
	<script type="text/javascript" src="js/angular.min.js"></script>

	<script type="text/javascript" src="js/angular-ui-router.min.js"></script>

	<script type="text/javascript" src="controller/StudentController.js"></script>

	<script type="text/javascript" src="app/app.js"></script>
<body>
	<div ui-view="content">
		
	</div>
</body>
</html>

template 

 addStudent.html

	<div>

		<form>

			姓名:<input type="text" ng-model="formData.name" >

			年龄:<input type="number" ng-model="formData.age">

			性别:<input type="radio" name="gender" ng-model="formData.gender" value="0">女
				 <input type="radio" name="gender" ng-model="formData.gender" value="1">男

			<button ng-click="add()">添加</button>
		</form>
	</div>

showStudent.html

	<div>

		<table>

			<thead>

				<tr>

					<th>姓名</th>

					<th>年龄</th>

					<th>性别</th>

				</tr>

			</thead>

			<tbody>

				<tr ng-repeat="item in datas">

					<td>{{item.name}}</td>

					<td>{{item.age}}</td>

					<td>{{item.gender}}</td>

					<td><a ng-click="delete(item.id)">删除</a></td>

					<td><a ui-sref="Main.getStudent({id:item.id})">更新</a></td>
				</tr>

			</tbody>

		</table>

	</div>

main.html

	<div>
		<a ui-sref="Main.addStudent">添加</a>
		<a ui-sref="Main.queryStudent">查询</a>
	</div>
	<div ui-view="Main_content">
		
	</div>

UpdateStudent.html

	<div>

		<form>

			姓名:<input type="text" ng-model="formData.name" >

			年龄:<input type="number" ng-model="formData.age">

			性别:<input type="radio" name="gender" ng-model="formData.gender" value="0">女
				 <input type="radio" name="gender" ng-model="formData.gender" value="1">男

			<button ng-click="">保存</button>
		</form>
	</div>

 app.js

//千万不要忘了注册Controller
var m = angular.module('myApp',['ui.router','StudentController']);
m.config(function($stateProvider,$urlRouterProvider){

	 $urlRouterProvider.otherwise("/Main/queryStudent");

	$stateProvider.state("Main",{

		url:'/Main',

		views:{

			"content":{

				templateUrl:"templates/main.html"

			}

		}

	}).state("Main.queryStudent",{

		url:'/queryStudent',

		views:{

			"Main_content@Main":{

				controller:'ShowStudentCNTR',

				templateUrl:"templates/showStudent.html"

			}

		}

	}).state("Main.addStudent",{

		url:'addStudent',

		views:{

			"Main_content@Main":{

				controller:'AddStudentCNTR',

				templateUrl:"templates/addStudent.html"

			}

		}

	}).state("Main.getStudent",{

		url:'/getStudent/{id}',

		views:{

			"Main_content@Main":{

				controller:'GetStudentCNTR',

				templateUrl:'templates/UpdateStudent.html'

			}

		}
	});


});






 StudentController.js

//牛逼的双向数据绑定
var m = angular.module("StudentController",[]);

m.controller("GetStudentCNTR",function($scope,$stateParams){

	var id = $stateParams.id;

	alert(id);
});

m.controller("AddStudentCNTR",function($scope){

	$scope.formData={

		gender:0

	}

     $scope.add = function(){

     		alert($scope.formData.name)
     		alert($scope.formData.age);
     		alert($scope.formData.gender);

     }

});

m.controller("ShowStudentCNTR",function($scope){

	$scope.datas = [{id:1,name:'哈哈哈',age:18,gender:'男'},{id:2,name:'哈哈2',age:18,gender:'男'},{id:3,name:'哈哈3',age:18,gender:'男'},{id:4,name:'哈哈哈',age:18,gender:'男'},{id:1,name:'哈哈哈',age:18,gender:'男'},{id:1,name:'哈哈哈',age:18,gender:'男'},{id:1,name:'哈哈哈',age:18,gender:'男'},{id:1,name:'哈哈哈',age:18,gender:'男'}];

	var pos = 0;

     $scope.delete = function(id){

     	for(var i=0;i<$scope.datas.length;i++){


     			var ob = $scope.datas[i];

     			if(ob.id==id){

     				pos = i;

     				break;

     			}


     	}

     	$scope.datas.splice(pos, 1);

        $state.go("Main");


     }


});

 

 

 源码点此下载 密码:enht

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值