angularJs $resource自定义方法(待完善)
配置CompanyService
1 var services = angular.module('liaoyuan.services'); 2 3 services.factory('CompanyService', function ($resource, baseApiUrl) { 4 return $resource(baseApiUrl, {}, { 5 query: { 6 method: "GET", 7 url: baseApiUrl + 'companys/:pageIndex1/:pageSize1', 8 params: {}, 9 isArray: false 10 }, 11 create: { method: "POST" }, 12 get: { method: "GET", url: baseApiUrl + "companys/?id=:id" }, 13 remove: { method: "DELETE", url: baseApiUrl + "companys/?id=:id" }, 14 update: { method: "PUT", url: baseApiUrl + 'companys/?id=:id' } 15 }) 16 });
method 请求后台的http方法;
params 参数;
isArray 是否以数组的方式返回,这里不对,前台会报错误 [$resource:badcfg]
CompanyController
1 app.controller('CompanyListController', function ($scope, CompanyService, FilterService, $uibModal) { 2 $scope.FilterService = FilterService; 3 4 $scope.page = { 5 "pageIndex": 1, "pageSize": 10, "totalCount": 0 6 }; 7 8 var queryArgs = { 9 pageIndex1: $scope.page.pageIndex, 10 pageSize1: $scope.page.pageSize 11 }; 12 13 CompanyService.query(queryArgs).$promise.then(function (result) { 14 $scope.companys = result.data; 15 $scope.page.totalCount = result.totalCount; 16 }, function (result) { //如果失败 17 console.log('fail'); 18 }); 19 20 21 //详情页弹窗 22 $scope.detail = function (com,size) { 23 var uibModalInstance = $uibModal.open({ 24 templateUrl: '/views/biz/company/detailModal.html', 25 controller: 'CompanyDetailsController',//为$modal指定的控制器,初始化$scope,该控制器可用$modalInstance注入 26 size: size, 27 backdrop: 'static', 28 keyboard: false, 29 //scope:,//一个作用域为模态的内容使用(事实上,$modal会创建一个当前作用域的子作用域)默认为$rootScope 30 resolve: { 31 company: function () { return com; } 32 },//定义一个成员并将他传递给$modal指定的控制器,相当于routes的一个reslove属性,如果需要传递一个objec对象,需要使用angular.copy() 33 //windowClass:,//指定一个class并被添加到模态窗口中 34 }); 35 }; 36 37 38 });