将参数传递给与 $http.get() 一起使用的服务
Passing an argument to a service for use with $http.get()
我创建了一个使用 API 的服务。我需要使用从表单上的文本输入传入的参数从我的控制器调用此服务。
myAppServices.factory('apiService', function ($http, $q) {
return {
getDocuments: function () {
return $http.get('/api/documents/',
{
params: {
id: '2' // this should not be hardcoded
}
})
.then(function (response) {
if (typeof response.data == 'object') {
return response.data;
} else {
// invalid response
return $q.reject(response.data);
}
}, function (response) {
// something went wrong
return $q.reject(response.data);
});
},
}
我的控制器目前看起来像这样...
myApp.controller('homeController', function ($scope, apiService) {
var docs = function () {
apiService.getDocuments()
.then(function (data) {
$scope.docs = data; //console.log(data);
}, function (error) {
// promise rejected ... display generic no data found on table
console.log('error', error);
});
};
}
我的输入只是...
<input type="text" class="form-control" placeholder="Enter ID" ng-model="id">
如何将输入到此输入中的值获取到我的服务中,以便我可以 return 基于输入文本输入中的 ID 值的数据?
您可以在服务定义中传递额外的参数。然后当你在你的控制器中使用它时,你可以传递额外的值。
myAppServices.factory('apiService', function ($http, $q) {
return {
getDocuments: function (param1, param2) {
}
服务方法需要传参,有一些建议可以考虑
1-) 承诺 api 和服务方法。
使用 $q 的正确方法是自己使用延迟对象。你的服务可能是这样的。
myAppServices.factory('apiService', function ($http, $q) {
return {
getDocuments: function (id) {
var deferred = $q.defer();
$http({
method: 'GET',
url: '/api/documents/',
params: {id: id}
}).success(function (response) {
if (typeof response.data == 'object') {
deferred.resolve(response.data);
} else {
deferred.reject(response.data);
}
}).error(function (response) {
deferred.reject(response.data);
});
return deferred.promise;
}
}
})
2-) 你的控制器可能是这样的。
myApp.controller('homeController', function ($scope, apiService) {
$scope.getDocs = function () {
apiService.getDocuments($scope.id)
.then(function (data) {
$scope.docs = data; //console.log(data);
}, function (error) {
// promise rejected ... display generic no data found on table
console.log('error', error);
});
};
});
应该是这样的:
在你的控制器中
apiService.getDocuments($scope.id).then...
为您服务
getDocuments: function (idVar) {
return $http.get('/api/documents/',
{
params: {
id: idVar // this should not be hardcoded
}
})
我创建了一个使用 API 的服务。我需要使用从表单上的文本输入传入的参数从我的控制器调用此服务。
myAppServices.factory('apiService', function ($http, $q) {
return {
getDocuments: function () {
return $http.get('/api/documents/',
{
params: {
id: '2' // this should not be hardcoded
}
})
.then(function (response) {
if (typeof response.data == 'object') {
return response.data;
} else {
// invalid response
return $q.reject(response.data);
}
}, function (response) {
// something went wrong
return $q.reject(response.data);
});
},
}
我的控制器目前看起来像这样...
myApp.controller('homeController', function ($scope, apiService) {
var docs = function () {
apiService.getDocuments()
.then(function (data) {
$scope.docs = data; //console.log(data);
}, function (error) {
// promise rejected ... display generic no data found on table
console.log('error', error);
});
};
}
我的输入只是...
<input type="text" class="form-control" placeholder="Enter ID" ng-model="id">
如何将输入到此输入中的值获取到我的服务中,以便我可以 return 基于输入文本输入中的 ID 值的数据?
您可以在服务定义中传递额外的参数。然后当你在你的控制器中使用它时,你可以传递额外的值。
myAppServices.factory('apiService', function ($http, $q) {
return {
getDocuments: function (param1, param2) {
}
服务方法需要传参,有一些建议可以考虑
1-) 承诺 api 和服务方法。
使用 $q 的正确方法是自己使用延迟对象。你的服务可能是这样的。
myAppServices.factory('apiService', function ($http, $q) {
return {
getDocuments: function (id) {
var deferred = $q.defer();
$http({
method: 'GET',
url: '/api/documents/',
params: {id: id}
}).success(function (response) {
if (typeof response.data == 'object') {
deferred.resolve(response.data);
} else {
deferred.reject(response.data);
}
}).error(function (response) {
deferred.reject(response.data);
});
return deferred.promise;
}
}
})
2-) 你的控制器可能是这样的。
myApp.controller('homeController', function ($scope, apiService) {
$scope.getDocs = function () {
apiService.getDocuments($scope.id)
.then(function (data) {
$scope.docs = data; //console.log(data);
}, function (error) {
// promise rejected ... display generic no data found on table
console.log('error', error);
});
};
});
应该是这样的:
在你的控制器中
apiService.getDocuments($scope.id).then...
为您服务
getDocuments: function (idVar) {
return $http.get('/api/documents/',
{
params: {
id: idVar // this should not be hardcoded
}
})