I am new with angular and working through some problems. While I know this is a small issue i'm struggling with the syntax on how to pass my parameter to my web service from my factory. My error is from the web service is the parameter id is missing.
Here is my config state:
.state("productDetail", {
url:"/products/:productId",
templateUrl: "app/products/productDetailView.html",
controller: "ProductDetailCtrl as vm",
resolve: {
productResource: "productResource",
product: function (productResource, $stateParams) {
var productId = $stateParams.productId;
return productResource.getById.get({ productId: productId }).promise;
}
}
And here is my angular resource:
angular
.module("commonservices")
.factory("productResource", ["$resource", function($resource){
return {
getAll:$resource("WebService.asmx/GetAll"),
getById: $resource("WebService.asmx/GetById/:productId", { productId: '@productId' })
};
}]);
Your code looks fine, only the case when it will break is, if you don't have value for productId
in $stateParams
, try check that value by doing console.log($stateParams.productId)
.
Also you had a typo while returning a promise from $resource
object. It should .$promise
instead of .promise
return productResource.getById.get({ productId: productId }).$promise
Edit
If you wanted to pass variable to webmethod, it should be passed as query parameter rather than pass it in URL
directly by assuming it. For that change your resource like below
getById: $resource("WebService.asmx/GetById", { });
And then change your parameter name to id
as it the name which we are passing to webservice. It should be passed as query parameter, then only that parameter will get read by WebMethod
.
return productResource.getById.get({ id: productId }).$promise
will form URL like WebService.asmx/GetById?id=1
(1 is assumed productId).