angular--用mock模拟数据 发表于 2017-03-30 应用场景当前端写好页面,而后台没有提供接口和数据时,可以先把获取数据的service做好,传入json类型的mock数据来模拟调取数据的情况。12345678910111213141516171819202122//模拟权限角色数据的获取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数据123456789101112131415161718192021222324252627282930313233343536373839{ "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"} 最后,我们只需要调用相应的服务,获取请求成功状态的数据就行了