I am quite new to Angular and I cant make this factories work, im just consuming a web service in another instance of visual studio. here is the code:
main factory:
nngmodule.factory('generaldataFactory', ['$http', function ($http, $httpProvider) {
var dataFactory = {};
dataFactory.getMethodWebService = function (pMethod, pUrl, pCache, pTimeout, pParams, pFunctionSuccess, pFunctionError) {
$http({
method: pMethod, url: pUrl, cache: pCache, timeout: pTimeout, params: pParams
}).then(pFunctionSuccess, pFunctionError);
};
return dataFactory;
}]);
personal factory (uses above one)
ngmodule.factory('miFactory', ['$http', function (generaldataFactory) {
var miFact = {};
miFact.GetNoticiasList = function (functionSuccess, functionError) {
return generaldataFactory.getMethodWebService("GET", 'http://localhost:40900/Noticias/GetNoticiasList', false, 25000, {}, functionSuccess, functionError);
};
miFact.FiltrarNoticias = function (id, functionSuccess, functionError) {
return generaldataFactory.getMethodWebService("GET", 'http://localhost:40900/Noticias/GetNoticiaById/', false, 25000, { 'id': id }, functionSuccess, functionError);
};
return miFact;
}]);
Controller:
ngmodule.controller('miController', function(miFactory) { var scope = this;
var registerSuccess = function (response) {
}
var registerError = function (response) {
}
scope.noticiasList = {}
scope.noticiasList = miFactory.GetNoticiasList(registerSuccess,registerError);
});
Error:
TypeError: generaldataFactory.getMethodWebService is not a function
at Object.miFact.GetNoticiasList (MiFactory.js:6)
at new <anonymous> (controllers.js:13)
at Object.invoke (angular.js:4478)
at extend.instance (angular.js:9136)
at nodeLinkFn (angular.js:8248)
at compositeLinkFn (angular.js:7680)
at compositeLinkFn (angular.js:7684)
at publicLinkFn (angular.js:7555)
at angular.js:1662
at Scope.$eval (angular.js:15989)
getMethodWebServicecan just do:return $http(...);andGetNoticiasListbecomesreturn generaldataFactory.getMethodWebService("GET", 'http://localhost:40900/Noticias/GetNoticiasList', false, 25000, {});and you call that like:miFactory.GetNoticiasList().then(function(response) { scope.noticiasList = {}; }).catch(registerError);All the callbacks just melt away.