页面部分大致如下:

<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资源的思路的更多相关文章

  1. AngularJS使用OData请求ASP.NET Web API资源的思路

    本篇整理AngularJS使用OData请求ASP.NET Web API资源的思路. 首先给ASP.NET Web API插上OData的翅膀,通过NuGet安装OData. 然后,给control ...

  2. .Net Core使用HttpClient请求Web API注意事项

    HttpClient 使用HttpClient可以很方便的请求Web API,但在使用时有一些需要注意的地方,不然会给你的程序带来毁灭性的问题. HttpClient是一个继承了IDisposable ...

  3. 动态枢轴网格使用MVC, AngularJS和WEB API 2

    下载shanuAngularMVCPivotGridS.zip - 2.7 MB 介绍 在本文中,我们将详细介绍如何使用AngularJS创建一个简单的MVC Pivot HTML网格.在我之前的文章 ...

  4. [angularjs] MVC + Web API + AngularJs 搭建简单的 CURD 框架

    MVC + Web API + AngularJs 搭建简单的 CURD 框架 GitHub 地址:https://github.com/liqingwen2015/Wen.MvcSinglePage ...

  5. ajax GET和POST请求web api 的几种方式

    GET请求 1.无参数get请求 一般get请求有两种写法,一种是 $.get()   一种是$.ajax({type:"get"}), 我个人比较喜欢用后者. 下面例子主要是ge ...

  6. angularjs呼叫Web API

    今早有分享一篇<创建Web API并使用>http://www.cnblogs.com/insus/p/7771428.html 接下来,我再分享一篇,怎样在angularjs去呼叫Web ...

  7. MUI 跨域请求web api

    由于刚接触MUI框架,所以在跨域问题上花了一点时间.希望我的方式能帮你少走点弯路(大神就直接过里吧)! 首先,遇到这个问题,各种百度.其中说法最多的是将mui,js文件里的 setHeader('X- ...

  8. 使用 HttpClient 请求 Web Api

    1.获取 post 请求 body 内容 [HttpPost] public string GetId() { //如果方法参数里面有 [FromBody],则需要重新调整内容指针,再进行读取. // ...

  9. C# 请求Web Api 接口,返回的json数据直接反序列化为实体类

    须要的引用的dll类: Newtonsoft.Json.dll.System.Net.Http.dll.System.Net.Http.Formatting.dll Web Api接口为GET形式: ...

随机推荐

  1. MySQL 5.6 Replication 复制 FAQ

    原文请参照MySQL官方文档Reference Manual,版本5.6.10. 复制功能使得数据可以从一个MySQL数据库(master主库)复制到另一个或多个MySQL数据库(slave从库).缺 ...

  2. Shiro:授权控制

    对容易忽略的地方记录如下: 1.需要引入下面2个依赖,具体版本根据自身环境修改: <dependency> <groupId>org.apache.geronimo.bundl ...

  3. 钉钉机器人-实现监控通知功能-python

    1. 首先得创建有 一个 钉钉群.(因为只能发群通知) 2. 添加机器人,得到一个url: 3. 开始写Python脚本: # -*- coding: utf-8 -*- ""&q ...

  4. 织梦任意页面调用{dede:field.content/}的方法

    过滤掉所有的html代码,只显示文字,具体的ID自己更改. 代码如下: {dede:sql sql='Select content from dede_arctype where id=1'} [fi ...

  5. JSR教程2——Spring MVC数据校验与国际化

    SpringMVC数据校验采用JSR-303校验. • Spring4.0拥有自己独立的数据校验框架,同时支持JSR303标准的校验框架. • Spring在进行数据绑定时,可同时调用校验框架完成数据 ...

  6. 实现C语言字符串操作的库函数 包括基本的字符串复制 字符串长度 字符串比较等多种函数(C代码)

    头文件 "mystring.h" #ifndef _MYSTR_H #define _MYSTR_H #include <stdio.h> #include <s ...

  7. SpringMVC框架04——RESTful入门

    1.RESTful的基本概念 REST(Representational State Transfer)表述性状态转移,REST并不是一种创新技术,它指的是一组架构约束条件和原则,符合REST的约束条 ...

  8. 多线程学习笔记四之Condition实现分析

    目录 简介 等待队列 nextWaiter 源码分析 await() signal() signalAll() 总结 简介   在使用内置锁synchronized时,通过调用java.lang.Ob ...

  9. 使用ApiPost测试接口时需要先登录的接口怎么办(基于Cookie)?

    在后台在开发.调试接口时,常常会遇到需要登陆才能请求的接口. 比如:获取登陆用户的收藏列表,此时,我们就需要模拟登陆状态进行接口调试了.如图: 今天,我们讲解利用ApiPost的环境变量,解决这种需要 ...

  10. mysql创建索引笔记

    1.添加PRIMARY KEY(主键索引.就是 唯一 且 不能为空.): ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQU ...