저는 Angular를 처음 접했지만 $ http에서 팩토리 / 리소스로 RESTful 호출을 추상화하려고하지만 매개 변수를 전달할 수없는 것 같습니다. 나는 그렇게 읽었지만 이것의 예를 찾을 수 없습니다.
내 공장 코드 (services.js) :
myApp.factory("PropertyDb", function($resource, $log) {
return {
getProperties: function(onSuccess) {
var properties = $resource("http://myurl.com/get_properties.php?", {
callback: 'JSON_CALLBACK',
postcode: 'AA11AA',
minimum_beds: '3',
minimum_price: '97500'
},
{
fetch : {method:'JSONP'},
params: {postcode: 'BB11BB'} // This doesn't override the above or work without the above postcode
});
properties.fetch(
function success(response) {
console.log(response);
onSuccess(response.listing);
},
function error(response) {
console.log(response);
console.log("error");
}
);
},
내 컨트롤러 코드 :
myControllers.controller('PropertyListCtrl', ['$scope', 'PropertyDb',
function($scope, PropertyDb) {
$scope.properties = {};
// Adding the postcode below doesnt work...
PropertyDb.getProperties({postcode : 'CC11CC'}, function(responce) {
$scope.properties = responce;
});
}]);
컨트롤러에서 공장을 사용하고 우편 번호 등과 같은 다른 매개 변수를 전달하고 공장에서 설정된 기본값을 재정의 할 수 있기를 원합니다. 내가 무엇을 시도해도 나는 이것을 할 수 없으며 문서는 따라 가기가 쉽지 않습니다.
귀하의 예에서 2 개의 매개 변수를 PropertyDb.getProperties
다음으로 전달했습니다 .
postcode
목적: {postcode : 'CC11CC'}
function(responce) {$scope.properties = responce;}
한 가지는 공장에서 첫 번째 매개 변수를 사용하는 것입니다.
myApp.factory("PropertyDb", function($resource, $log) {
return {
getProperties: function(parameter, onSuccess) {
// ^param^ , ^callback^
/* ... */
}
따라서 고정 된 서비스 버전은 다음과 같아야합니다.
myApp.factory("PropertyDb", function($resource, $log) {
return {
getProperties: function(parameter, onSuccess) {
var properties = $resource("http://myurl.com/get_properties.php?", {
callback: 'JSON_CALLBACK',
postcode: parameter.postcode,
minimum_beds: '3',
minimum_price: '97500'
},
{
fetch : {method:'JSONP'},
params: parameter
});
properties.fetch(
function success(response) {
console.log(response);
onSuccess(response.listing);
},
function error(response) {
console.log(response);
console.log("error");
}
);
},
/*...*/
}
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다