AnjularJs的增删改查(单页网站)
2016.6.4
学习文献:
你的第一个AngularJS应用:https://segmentfault.com/a/1190000000347412
AngularJS 提交表单的方式:http://www.oschina.net/translate/submitting-ajax-forms-the-angularjs-way
AngularJS中$http服务的简单用法:http://www.2cto.com/kf/201506/405137.html



代码由3块实现:
1.Ui
mvc5的解释就是定义项目,定义模块,定义基本视图
<body ng-app='routingDemoApp'>
<div class="container">
<div class="row">
<h1 class="text-center">angular 单页新闻发布系统</h1>
</div>
<div class="row">
<div class="col-md-2">
<ul class="">
<li><a href="#/put">发布</a></li>
<li><a href="#/list">新闻列表</a></li>
</ul>
</div>
<div class="col-md-10" ng-view></div>
</div>
</div>
</body>
2.AnjularJs的功能实现
<script>
//配置路由器,绑定视图和控制器,细节看上面引用链接
var app = angular.module('routingDemoApp', ['ngRoute']);
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'list.html',
controller: 'ListController'
})
.when('/list', {
templateUrl: 'list.html',
controller: 'ListController'
})
.when('/edit/:id', {
templateUrl: 'edit.html',
controller: 'EditController'
})
.when('/del/:id', {
controller: 'DelController',
templateUrl: 'del.html',
})
.when('/detail/:id', {
templateUrl: 'detail.html',
controller: 'DetailController'
})
.when('/put', {
templateUrl: 'put.html',
controller: 'PutController'
})
.otherwise({ redirectTo: '/' });
}]);
//写控制器功能
app.controller("ListController", function ($scope, $http) {
$http.get("/AngularJS/data.ashx?action=getall")
.success(function (data) {
$scope.models = data.models;
})
});
app.controller("PutController", function ($scope, $http) {
$scope.addcontent = function () {
//这种写法很差,建议看下面EditController控制器里面用对象来写值
var data = "title=" + $scope.title + "&author=" + $scope.author + "&content=" + $scope.content;
$http.get("/AngularJS/data.ashx?action=add&" + data)
.success(function (data ) {
if (data == && confirm("发布成功是否跳转")) location.href = "";
});
}
});
app.controller("EditController", function ($scope, $http, $routeParams) {
$scope.updata = function () {
//nnd,Jq用的 ("form").serialize(),只能拼接了;想用$http.get(url,congfig)的,但是值传不过去。。。
$scope.model.action = "up";
$http({ method: "get", url: "/AngularJS/data.ashx", params: $scope.model })
.success(function (data) {
if (data != ) return false;
location.href = "";
});
}; $http.get("/AngularJS/data.ashx?action=get&id=" + $routeParams.id)
.success(function (data) {
$scope.model = data;
})
.error(function () {
alert("error");
$scope.formData = {};
}); });
app.controller("DelController", function ($scope, $http, $routeParams) {
$http.get("/AngularJS/data.ashx?action=del&id=" + $routeParams.id)
.success(function (data) {
if (data != ) return false;
window.location.href("./");
})
.error(function (data) {
alert("error");
})
});
app.controller("DetailController", function ($scope, $http, $routeParams) {
$http.get("/AngularJS/data.ashx?action=get&id=" + $routeParams.id)
.success(function (data) {
$scope.model = data;
})
.error(function () {
alert("失败");
})
$scope.updata = function () {
$http.get("/AngularJS/data.ashx?action=up")
}
}); </script>
3.视图模板
<script type="text/ng-template" id="put.html">
<form class="form-horizontal">
<div class="form-group">
<label>标题</label>
<input type="text" class="form-control" ng-model="title" />
</div>
<div class="form-group">
<label>作者</label>
<input type="text" class="form-control" ng-model="author" />
</div>
<div class="form-group">
<label>内容</label>
<textarea type="text" class="form-control" ng-model="content"> </textarea>
</div>
<div class="form-group">
<input type="button" class="btn btn-success form-control" value="提交" ng-click="addcontent()"/>
</div>
</form>
</script> <script type="text/ng-template" id="list.html">
<div class="row" >
<table class="table">
<thead>
<tr>
<th>编号</th>
<th>标题</th>
<th>时间</th>
<th>管理</th>
</tr>
</thead>
<tbody ng-repeat="x in models">
<tr>
<td>{{ $index+ }}</td>
<td><a href="#/detail/{{x.Id}}"> {{ x.title }}</a></td>
<td>{{ x.time }}</td>
<td><a href="#/edit/{{x.Id}}">编辑</a>|<a href="#/del/{{x.Id}}">删除</a></td>
</tr>
</tbody>
</table>
</div>
</script> <script type="text/ng-template" id="edit.html">
<form class="form-horizontal">
<input type="hidden" ng-model="model.Id"/>
<div class="form-group">
<label>标题</label>
<input type="text" class="form-control" ng-model="model.title" />
</div>
<div class="form-group">
<label>作者</label>
<input type="text" class="form-control" ng-model="model.author" />
</div>
<div class="form-group">
<label>内容</label>
<textarea type="text" class="form-control" ng-model="model.content"> </textarea>
</div>
<div class="form-group">
<input type="button" class="btn btn-success form-control" value="修改" ng-click="updata()"/>
</div>
</form>
</script> <script type="text/ng-template" id="detail.html">
<h1 class="text-center">{{ model.title }}</h1>
<span class="text-center">作者:{{ model.author }},时间:{{ model.time }}</span>
<div>
{{ model.content }}
</div>
</script> <script type="text/ng-template" id="del.html"> </script>
开发遇到以下几个问题:
1.Post提交的data数据是什么格式了?
JQ:$.post("url",data"")
Ang:$http.Post(data:"",params:"")
2.$scope.model获取前台传来的Json的取值问题
$http.get(url).success(function(data){
$scope.model = data;
})
json为集合:{"obj":[{id:1,name:"zzj"},{id:1,name:"zzj"}]},使用就是:$scope.model.obj[0].id,$scope.model.obj[0].name
json为对象:{id:1,name:"zzj"},使用的时候就是 $scope.model.id, $scope.model.name
3.路由器配置问题
http://www.runoob.com/angularjs/angularjs-routing.html
4.json转obj,string的细节(json检验网:http://www.bejson.com/)
json有2种:对象、集合
对象:{key:value,key:value}
集合:{object:[{key:value},{key:value}]}
格式之间的互转:
在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法。
JSON.stringify(obj)将JSON转为字符串。
JSON.parse(string)将字符串转为JSON格式;
Angular.fromJson( data );
Angular.toJson(data);
AnjularJs的增删改查(单页网站)的更多相关文章
- day 68 增删改查 语法
1 普通正则 2 分组正则 url(r'/blog/(\d+)/(\d+)',views.blog) blog(request,arq1,arq2) 按照位置传参 3 分组命名 url(r'/ ...
- python全栈开发day61-django简单的出版社网站展示,添加,删除,编辑(单表的增删改查)
day61 django内容回顾: 1. 下载: pip install django==1.11.14 pip install -i 源 django==1.11.14 pycharm 2. 创建项 ...
- Django学习笔记(10)——Book单表的增删改查页面
一,项目题目:Book单表的增删改查页面 该项目主要练习使用Django开发一个Book单表的增删改查页面,通过这个项目巩固自己这段时间学习Django知识. 二,项目需求: 开发一个简单的Book增 ...
- Django框架之第二篇--app注册、静态文件配置、form表单提交、pycharm连接数据库、django使用mysql数据库、表字段的增删改查、表数据的增删改查
本节知识点大致为:静态文件配置.form表单提交数据后端如何获取.request方法.pycharm连接数据库,django使用mysql数据库.表字段的增删改查.表数据的增删改查 一.创建app,创 ...
- 05_Elasticsearch 单模式下API的增删改查操作
05_Elasticsearch 单模式下API的增删改查操作 安装marvel 插件: zjtest7-redis:/usr/local/elasticsearch-2.3.4# bin/plugi ...
- Elasticsearch 单模式下API的增删改查操作
<pre name="code" class="html">Elasticsearch 单模式下API的增删改查操作 http://192.168. ...
- 关于单链表的增删改查方法的递归实现(JAVA语言实现)
因为在学习数据结构,准备把java的集合框架底层源码,好好的过一遍,所以先按照自己的想法把单链表的类给写出来了; 写该类的目的: 1.练习递归 2.为深入理解java集合框架底层源码打好基础 学习的视 ...
- 1.SSM整合_单表的增删改查
目标:增删改查 环境:Maven+Eclipse+Tomcat7+JDK7 思维导图: 表结构 目录结构 依赖 <dependencies> <dependency> < ...
- $Django orm增删改字段、建表 ,单表增删改查,Django请求生命周期
1 orm介绍 ORM是什么 ORM 是 python编程语言后端web框架 Django的核心思想,“Object Relational Mapping”,即对象-关系映射,简称ORM. 一 ...
随机推荐
- JavaScript学习04 对象
JavaScript学习04 对象 默认对象 日期对象Date, 格式:日期对象名称=new Date([日期参数]) 日期参数: 1.省略(最常用): 2.英文-数值格式:月 日,公元年 [时:分: ...
- java "".split(",")
String[] string = "".split(","); 结果是string = []; String[] string = " " ...
- php设计模式 观察者模式
观察者模式的核心是把客户元素(观察者)从一个中心类(主体)中分离开来.当主体知道事件发生时,观察者需要被通知到.同时,我们并不希望将主体与观察者之间的关系进行硬编码.为了达到这个目的,我们可以允许观察 ...
- IntelliJ IDEA 12.1.4 解决中文乱码
一.进入IDE Settings 里的 Appearance项,选中Override default fonts by ,把 Name 设置为 SimSun,Size 根据自己喜好设置(我一般设为 1 ...
- DbUtils是Apache出品一款简化JDBC开发的工具类
DbUtils - DbUtils是Apache出品一款简化JDBC开发的工具类 - 使用DbUtils可以让我们JDBC的开发更加简单 - DbUtils的使用: ...
- asp.net开发中常见公共捕获异常方式总结(附源码)
本文实例总结了asp.net开发中常见公共捕获异常方式.分享给大家供大家参考,具体如下: 前言:在实际开发过程中,对于一个应用系统来说,应该有自己的一套成熟的异常处理框架,这样当异常发生时,也能得到统 ...
- git diff的用法
在git提交环节,存在三大部分:working tree(工作区), index file(暂存区:stage), commit(分支:master) working tree:就是你所工作在的目录, ...
- 13、系统集成项目经理要阅读的书籍 - IT软件人员书籍系列文章
系统集成项目经理主要对弱电等项目负责.一般包括计算机网络系统,计算机设备系统,智能楼宇,机房建设等.在软考中,系统集成项目经理放在了中级,这个只能说明系统集成项目经理需要的经验没有高级信息系统项目管理 ...
- 【转发】Html5 File Upload with Progress
Html5 File Upload with Progress Posted by Shiv Kumar on 25th September, 2010Senior Sof ...
- 【SQL篇章--CREATE TABLE】
[SQL篇章][SQL语句梳理 :--基于MySQL5.6][已梳理:CREATE TABLE][会坚持完善] SQL : 1. Data Definition Statements: 1.3 CRE ...