AnjularJs的增删改查(单页网站)

news/2024/5/20 16:09:15/文章来源:https://blog.csdn.net/weixin_30649859/article/details/95095620

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>
View Code

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 == 1 && 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 != 1) 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 != 1) 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>
View Code

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+1 }}</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>
View Code

 

开发遇到以下几个问题:

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);

转载于:https://www.cnblogs.com/0to9/p/5560393.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_823293.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mouseover,mouseout与mouseenter,mouseleave

针对单个元素&#xff0c;使用感一样。 差异提现在有子元素的情况下&#xff1a; mouseover和mouseout在父元素和其子元素都可以触发&#xff0c;当鼠标穿过一个元素时&#xff0c;触发次数得依子元素数量而言。mouseenter和mouseleave只在父元素触发&#xff0c;当鼠标穿过一个…

如何让浏览器打开一个网站的时候执行一个自定义脚本

以chrome为例&#xff0c;先安装一个tampermonkey的插件&#xff0c;安装好之后点击一下按钮&#xff0c;添加新脚本,写入如下代码之后保存。 第七行的意思是&#xff0c;当我们打开百度的时候才会执行这个脚本。脚本的作用就是弹出一个消息。 转载于:https://www.cnblogs.com/…

docker (2)---存储、网络(利用docker容器上线静态网站)

一、docker底层依赖的核心技术 1、命名空间 (Namespaces) 2、控制组 (Control Groups) 3、联合文件系统 (Union File System) 4、Linux 虚拟网络支持&#xff1a;本地和容器内创建虚拟接口 (1) 命名空间(Namespaces)&#xff1a; 实现了容器间资源的隔离&#xff0c;每个容器…

从QQ网站中提取的纯JS省市区三级联动

今天收到园友信息&#xff0c;想问我要原来写的一个 《 纯JS省市区三级联动 》文章中最新的省市区数据。 那个是老早以前搞的。记得数据是从数据库中提取生成的。一时也找不到当时的数据库了。 我发现在 http://ip.qq.com/ 的网站中有QQ自己的JS省市区三级联动。所以研究了一下…

无需注册支持快速安全视频的网站

大家好&#xff0c;我是你们的小金子。今天要分享的是一个无需注册支持快速安全视频的网站&#xff0c;既然说到快速&#xff0c;那么我们先来看看到底有多快速。首先打开网站 https://brie.fi/ng/ 会展示如下网页。点击 Start Video Chat&#xff0c;即可直接开始视频。分享右…

Google 出品网站性能质量及错误分析工具

大家好&#xff0c;我是你们的章鱼猫。今天给大家推荐一款由 Google 出品的 Chrome 插件&#xff1a;Lighthouse&#xff0c;它能够分析网站的性能、质量及错误等。市面上类似工具很少&#xff0c;可以看出越大的公司对性能和质量越看重。这款工具通过 Chrome 安装后使用也非常…

GitHub Star 10K,让你的网站更炫酷的开源库

大家好&#xff0c;我是你们的章鱼猫。现在不少网站都支持了骨架屏&#xff0c;能够在网页数据加载前&#xff0c;展示固定的布局&#xff0c;能够减少用户在进入网页时感受到白屏的不适感。今天要给大家推荐一个标星 1 万的开源库&#xff1a;react-content-loader&#xff0c…

1 分钟提升网站使用体验,这么香的方法不想知道吗?

大家好&#xff0c;我是你们的章鱼猫。今天要推荐的开源项目是 instant.page&#xff0c;在介绍项目之前&#xff0c;我们先说说项目的介绍语&#xff0c;如下&#xff1a;Make your site’s pages instant in 1 minute and improve your conversion rate by 1%.什么意思呢&…

非常棒的练手项目, 一个基于 SpringBoot 开源的小说和漫画在线阅读网站

大家好&#xff0c;我是章鱼猫。今天给大家推荐的这个开源项目是一个基于 SpringBoot 实现的小说和漫画在线阅读网站。这个开源项目叫&#xff1a;fiction_house。这个开源项目是一个多平台&#xff08;web、安卓 app、微信小程序&#xff09;、功能完善的小说弹幕网站&#xf…

「硬核推荐」一份网站流量变现教程及资料大全

文章首发于微信公众号「GitHub精选」&#xff0c;欢迎大家关注。大家好&#xff0c;我是章鱼猫。今天推荐的这个项目是「awesome-seo」&#xff0c;Google SEO 研究及流量变现。作者为什么要做 SEO&#xff1f;在互联网中&#xff0c;流量就是钱。掌握一定的 SEO 技术&#xff…

天若有情天亦老,我为网站加一秒

文章首发于微信公众号「GitHub 精选」&#xff0c;欢迎大家关注。打开微信&#xff0c;使用 “搜一搜”&#xff0c;搜索「GitHub 精选」&#xff0c;即可关注。大家好&#xff0c;我是章鱼猫。今天推荐的这个项目是「1s」&#xff0c;有些网站&#xff0c;当我们给它 1s 时&am…

开发者论坛一周精粹(第三十六期) 网站备案 oss存储

开发者论坛每周选取精华内容总结&#xff0c;精选论坛优质贴&#xff0c;每周更新一期&#xff0c;方便大家阅读&#xff01; 依赖SDK时发生错误 ziyeyc http://bbs.aliyun.com/read/576757.html 域名认证&#xff0c;时间太慢了吧 追梦天使11 http://bbs.aliyun.com/read/57…

小爬虫demo——爬取“妹子”等网站链接____使用requests库

# 文章分为三个部分&#xff1a;1. po代码 &#xff1b;2. 提示运行过程中的一些细节 和需要改进的地方&#xff1b; 3. 常规送福利# 文章将持续完善和补充# 欢迎拍砖、交流、指正第一部分&#xff1a;Po 代码&#xff1a;import re import requestskey input("请输入爬…

知名工具网站-开发、设计

这两年收藏了不少网站&#xff0c;特地整理一下&#xff0c;把一些大家都可能用得上的分享出来&#xff0c;希望能对你有用。 考虑到有一些网站大多数人都知道&#xff0c;所以我就不列出来了。 我把这些网站分为了几大类&#xff1a; 工具类素材类社区类 工具类 1、start.…

HTTPS时代的到来是大势所趋!阿里云CDN如何助力企业网站进入HTTPS时代

摘要&#xff1a; 在2015年&#xff0c;天猫和淘宝已经实现了全站HTTPS&#xff0c;并且在2015年底&#xff0c;阿里云CDN HTTPS产品化并且开始全面对外&#xff0c;并且为越来越多的客户提供HTTPS服务。无论是从阿里内部还是外部的同行&#xff0c;还从是Google的Chrome以及Mo…

在web项目中使用WebBrowser类-----给网站抓图

最近做一个WEB项目&#xff0c;其中要求有个功能就是程序能网页抓图&#xff0c;举个例子&#xff1a; 在test.aspx页面上放一个TextBox和一个Button&#xff0c;TextBox用来输入要抓取的网页地址&#xff0c;然后按了Button之后&#xff0c;服务器要对前面输入的网址进行抓图&…

【新手教程】阿里云视频点播,轻轻松松给网站加上视频的翅膀

您是不是被 网站 视频 问题 弄的焦头烂额、心烦意乱、夜不能寐、寝食难安&#xff1f;那么&#xff0c;看完这里&#xff0c;以上问题统统都可以解决啦。首先&#xff0c;我们开通 阿里云 视频点播功能&#xff0c;传送门&#xff1a; https://www.aliyun.com/product/vod开通需…

2018最受欢迎开源免费CMS建站系统排行榜

随着互联网及web应用技术的蓬勃发展&#xff0c;网上针对企业建站的各种CMS建站系统层出不穷&#xff0c;如今企业建站和维护的成本已不像过去那么高。经常在网上看见有人问及”哪个CMS系统最好用”、”企业建站用哪个CMS系统最多”等类似问题&#xff0c;所以本文将和大家一起…

记一次真实的网站被黑经历

前言 距离上次被DDOS攻击已经有10天左右的时间&#xff0c;距离上上次已经记不起具体那一天了&#xff0c;每一次都这么不了了只。然而近期一次相对持久的攻击&#xff0c;我觉得有必要静下心来&#xff0c;分享一下被黑的那段经历。 在叙述经历之前&#xff0c;先简单的介绍一…

开发者论坛一周精粹(第五十一期) 阿里云上建的网站国外能访问吗?

开发者论坛每周选取精华内容总结&#xff0c;精选论坛优质贴&#xff0c;每周更新一期&#xff0c;方便大家阅读&#xff01; &#xff08;钉钉&#xff09;IoT开发者俱乐部合伙人火热招募中 云栖青年 摘要&#xff1a;IoT开发者俱乐部&#xff0c;是阿里云IoT事业部发…