2
저는 AngularJS 웹 개발자입니다. 나는 toddmotto의 각도 스타일 가이드 (https://github.com/toddmotto/angularjs-styleguide)를 읽었습니다. 현재 RESTful API를 통해 데이터를 가져 오는 CRUD 앱을 만들고 있습니다.
저는 서비스 및/또는 공장에 조금 붙어 있습니다.
내가 이전에 작업했으나 (아래 코드) Todd의 스타일 가이드를보고 나서 컨트롤러에서 서비스/공장으로 논리를 옮기고 싶습니다.하지만 작업하기 전에 얻은 정보를 얻는 방법을 모르겠습니다. 새로운 구조.
아무도 아이디어가 있습니까?
이전 UserService.js 현재
(function() {
'use strict';
function UserService($resource) {
return $resource('https://api.path.com/users/:id', { }, {
query: {
method: 'GET'
},
create: {
method: 'POST'
},
update: {
method: 'PUT'
},
delete: {
method: 'DELETE'
}
});
}
angular
.module('app')
.factory('UserService', UserService);
})();
, 나는이 :
UserController.js
(function() {
'use strict';
function UserController(UserService) {
var _this = this;
UserService
.getUsers()
.then(function (response) {
_this.users = response;
console.table(response);
});
};
}
angular
.module('app')
.controller('UserController', UserController);
})();
UserService.js
(function() {
'use strict';
function UserService($resource) {
var service = {};
service.getUsers = function() {
return $resource('https://api.path.com/users');
};
return service;
}
angular
.module('app')
.factory('UserService', UserService);
})();
편집
var resource = $resource(baseUrl + '/users/:id', { }, {
query: {
method: 'GET'
},
create: {
method: 'POST'
},
update: {
method: 'PUT'
},
delete: {
method: 'DELETE'
}
});
service.getUsers = function() {
return resource.query().$promise;
};
감사합니다. 그 (일종의) 일했습니다. 배열 대신 객체를 수신 할 때 오류가 발생하면서 약간 수정했습니다. 내 새로운 코드는 위의 것입니다 - 이것이 이것을 달성하는 가장 좋은 방법이라고 생각하십니까? –
네, 괜찮아 보입니다. – dfsq
우수, 도움 주셔서 감사합니다! –