angular--用mock模拟数据

应用场景

当前端写好页面,而后台没有提供接口和数据时,可以先把获取数据的service做好,传入json类型的mock数据
来模拟调取数据的情况。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//模拟权限角色数据的获取
angular.module('app').factory('authRoleService', authRoleService);
authRoleService.$inject = ['$q', 'iHttpService'];
function authRoleService($q, iHttpService) {
return {
//根据类型获取角色列表
getRolesByType: getRolesByType,
};
function getRolesByType(_type) {
var defer = $q.defer();
//var url = "/api/v1/lawOffice/"+window.localStorage.officeid;
var url = "auth/mock/roles.json";
iHttpService.mockServer(url, {}, "get").then(function (data) {
defer.resolve(data.data);
}, function () {
defer.reject();
});
return defer.promise;
}
}

接下来提供mock数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
"data": [
{
"description": "我是负责人的描述",
"id": 0,
"name": "负责人",
"officeId": "string",
"permission": {},
"subjectId": "string",
"subjectType": "string",
"type": "string",
"valid": true
},
{
"description": "我是参与人的描述",
"id": 1,
"name": "参与人",
"officeId": "string",
"permission": {},
"subjectId": "string",
"subjectType": "string",
"type": "string",
"valid": true
},
{
"description": "我是实习生的描述",
"id": 2,
"name": "实习生",
"officeId": "string",
"permission": {},
"subjectId": "string",
"subjectType": "string",
"type": "string",
"valid": true
}
],
"resultCode": "string",
"resultMessage": "string"
}

最后,我们只需要调用相应的服务,获取请求成功状态的数据就行了