有关AngularJS请求Web API资源的思路
页面部分大致如下:
<body ng-app="productManagement">
...
<div ng-include="'app/products/productListView.html'"></div>
...
</body>
productManagement是页面module的名称。页面内容通过ng-include加载productListView.html这个页面。注意:ng-include属性值是字符串'app/products/productListView.html', 而不是app/products/productListView.html,这点很容易疏忽。
页面部分的JavaScript引用顺序通常是:有关AngularJS的文件、页面级别的js文件、自定义有关Service的js文件、具体有关controller的js文件,就像这样:
<!-- Library Scripts -->
<script src="scripts/angular.js"></script>
<script src="Scripts/angular-resource.js"></script>
<!-- Application Script -->
<script src="app/app.js"></script> <!-- Services -->
<script src="common/common.service.js"></script>
<script src="common/productResouce.js"></script>
<!-- Product Controllers -->
<script src="app/products/productListCtrl.js"></script>
在common.service.js中,定义了一个module:
(function () {
"use strict";
angular.module("common.services", ["ngResource"])
.constant("appSettings", {
serverPath: "http://localhost:49705/"
});
}());
以上,common.services这个module,依赖ngResource这个module,而ngResource这个module是被封装在angular-resource.js文件中的,由此,把有关AngularJS的文件放在顶部是有道理的,否则common.services这个module就引用不到了。另外,common.services这个module还定义了一个常量,key是appSettings, value是一个object对象,该对象的serverPath存储固定域名。
接下来,通过productResouce.js文件,自定义一个factory,用来返回具体的API路径。
(function () {
"use strict";
//通过工厂的方式创建了一个服务
angular.module("common.services")
.factory("productResource", ["$resource", "appSettings", productResouce]);
function productResouce($resource, appSettings) {
return $resource(appSettings.serverPath + "/api/products/:id");
}
}());
以上,为common.services这个module增加了一个factory,返回经$resource封装的API路径。同样$resource这个服务来自于,angular-resource.js文件,再次体会到把有关AngularJS的文件放在顶部是有道理的。
好,有了module,注册了factory,productListCtrl.js就来使用它们。
(function () {
"use strict";
angular
.module("productManagement")
.controller("ProductListCtrl",
ProductListCtrl);
function ProductListCtrl(productResource) {
var vm = this;
productResource.query(function (data) {
vm.products = data;
});
}
}());
以上,通过factory注册的service注入到这里,调用productResource.query方法把数据加载到model中来。
然后,productListView.html这个页面使用ProductListCtrl这个controller。
<div class="panel panel-primary"
ng-controller="ProductListCtrl as vm">
<div class="panel-heading"
style="font-size:large">
Product List
</div> <div class="panel-body">
<table class="table"> <thead>
<tr>
<td>Product</td>
<td>Code</td>
<td>Available</td>
<td>Price</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in vm.products">
<td>{{ product.productName}}</td>
<td>{{ product.productCode }}</td>
<td>{{ product.releaseDate | date }}</td>
<td>{{ product.price | currency }}</td>
</tr>
</tbody>
</table>
</div>
</div>
在页面级别的app.js文件中是这样的:
(function () {
"use strict";
var app = angular.module("productManagement", ["common.services"]);
}());
以上,productManagement依赖于自定义的common.services这个moduel。
总结:
1、js的位置关系要养成好的习惯:AngularJS文件,页面文件,自定义service文件,controller文件
2、把请求API的逻辑封装到自定义modlue中去,通过factory方式自定义service,使用$resouce来封装API请求路径
3、把自定义的service注入到controller中,请求API数据给Model
4、在主页面的module注册所有依赖的module
有关AngularJS请求Web API资源的思路的更多相关文章
- AngularJS使用OData请求ASP.NET Web API资源的思路
本篇整理AngularJS使用OData请求ASP.NET Web API资源的思路. 首先给ASP.NET Web API插上OData的翅膀,通过NuGet安装OData. 然后,给control ...
- .Net Core使用HttpClient请求Web API注意事项
HttpClient 使用HttpClient可以很方便的请求Web API,但在使用时有一些需要注意的地方,不然会给你的程序带来毁灭性的问题. HttpClient是一个继承了IDisposable ...
- 动态枢轴网格使用MVC, AngularJS和WEB API 2
下载shanuAngularMVCPivotGridS.zip - 2.7 MB 介绍 在本文中,我们将详细介绍如何使用AngularJS创建一个简单的MVC Pivot HTML网格.在我之前的文章 ...
- [angularjs] MVC + Web API + AngularJs 搭建简单的 CURD 框架
MVC + Web API + AngularJs 搭建简单的 CURD 框架 GitHub 地址:https://github.com/liqingwen2015/Wen.MvcSinglePage ...
- ajax GET和POST请求web api 的几种方式
GET请求 1.无参数get请求 一般get请求有两种写法,一种是 $.get() 一种是$.ajax({type:"get"}), 我个人比较喜欢用后者. 下面例子主要是ge ...
- angularjs呼叫Web API
今早有分享一篇<创建Web API并使用>http://www.cnblogs.com/insus/p/7771428.html 接下来,我再分享一篇,怎样在angularjs去呼叫Web ...
- MUI 跨域请求web api
由于刚接触MUI框架,所以在跨域问题上花了一点时间.希望我的方式能帮你少走点弯路(大神就直接过里吧)! 首先,遇到这个问题,各种百度.其中说法最多的是将mui,js文件里的 setHeader('X- ...
- 使用 HttpClient 请求 Web Api
1.获取 post 请求 body 内容 [HttpPost] public string GetId() { //如果方法参数里面有 [FromBody],则需要重新调整内容指针,再进行读取. // ...
- C# 请求Web Api 接口,返回的json数据直接反序列化为实体类
须要的引用的dll类: Newtonsoft.Json.dll.System.Net.Http.dll.System.Net.Http.Formatting.dll Web Api接口为GET形式: ...
随机推荐
- PowerDesigner显示Common注释列并自动赋值
PowerDesigner中默认不显示Common注释列,可根据以下步骤显示并紫东填充Name列内容. 1.显示Common注释列 2.运行VB Script脚本自动赋值 使用Shift+Ctrl+X ...
- VAE(Variational Autoencoder)的原理
Kingma, Diederik P., and Max Welling. "Auto-encoding variational bayes." arXiv preprint ar ...
- 走进异步编程的世界--async/await项目使用实战
起因:今天要做一个定时器任务:五分钟查询一次数据库发现超时未支付的订单数据将其状态改为已经关闭(数据量大约100条的情况) 开始未使用异步: public void SelfCloseGpPayOrd ...
- oracle数据库如何创建用户和角色,并给其赋权?
一.创建用户并赋予权限 1.创建用户 create user wangxiangyu identified by wangxiangyu; 2.赋权 grant dba to wangxiangyu; ...
- ckeditor:新增时会得到上次编辑的内容
参考网址:http://blog.sina.com.cn/s/blog_6961ba9b0102wwye.html 第一次新增时没有问题,编辑器里面内容为空,编辑数据时,也是正常,但是第二次点击新增时 ...
- android 8.0变更
Android 8.0 行为变更 Android 8.0 除了提供诸多新特性和功能外,还对系统和 API 行为做出了各种变更.本文重点介绍您应该了解并在开发应用时加以考虑的一些主要变更. 其中大部分变 ...
- 几个node项目实例-《转载》
1. 词搜索 根据一个特效匹配模式搜索整个英语词典词.这个程序是一个相当实在的应用.有足够的不平常代码,帮助你学习NodeJS应用架构以及如何使用NodeJS做一些有用的平台. 它使用expressw ...
- 解决Too many connections问题
有些人觉得,解决too many connections问题,灰非简单,down了mysql,修改my.cnf调大max_connections,好吧,你想法是没错的,这的确可以解决问题,但试问对于线 ...
- Android中如何在代码中设置View的宽和高?
Android中如何在代码中设置View的宽和高?https://zhidao.baidu.com/question/536302117.htmlhttps://blog.csdn.net/u0141 ...
- hdu 1698 线段树(成段替换 区间求和)
一条钩子由许多小钩子组成 更新一段小钩子 变成铜银金 价值分别变成1 2 3 输出最后的总价值 Sample Input11021 5 25 9 3 Sample OutputCase 1: The ...