Answer the question
In order to leave comments, you need to log in
What is the correct way to call a function in a factory?
Hello everyone, tell me how to get rid of this terrible error. there is a directive, its task is to display the object received from the request to the factory, which in turn receives information from $resource.
// директива должна вывести инфу из запроса от фабрики ProfileResource
(function() {
'use strict';
angular
.module('LenaPillars')
.directive('statusProfileDirective', statusProfileDirective);
statusProfileDirective.$injector = ['$log', 'ProfileResource'];
function statusProfileDirective($log, ProfileResource) {
return {
// controller: 'MainController',
restrict: 'E',
templateUrl: 'templates/directive/checkProfileService.html',
replace: true,
transclude: true,
scope: {
status: '&'
},
link: function ( scope, element, attrs) {
scope.statusus = getProfile();
console.log(scope.statusus);
}
}
function getProfile() {
return getProfileCheck().then(function() {
logger.info('Все прошло норм');
});
}
function getProfileCheck() {
return ProfileResource.then(function() {
return checkProfile.checkActiveProfile()
.then(function(data) {
console.log('gde ti moi data: '+data);
vm.avengers = data;
return vm.avengers;
});
});
}
}
})();
return ProfileResource.then(function() {
return checkProfile.checkActiveProfile()
ProfileResource.checkProfile.the(function() { .....
//но как в ней обратиться к checkActiveProfile ??? ужасно туплю, понимаю что то переставить и все должно работать
// ProfileResource фабрика с $resourse
(function() {
'use strict';
angular
.module('LenaPillars')
.factory('ProfileResource', ProfileResource);
ProfileResource.$injector = ['$rootScope', '$resource'];
function ProfileResource($rootScope, $resource) {
return {
checkProfile: checkProfile
}
function checkProfile() {
return $resource($rootScope.storeUrl+'/checkProfile&token='+$rootScope.token,
{
setting_profiles: 'setting_profiles'
},
{
'checkActiveProfile': {
method: 'GET', isArray: false
}
})
.then(getAvengersComplete)
.catch(getAvengersFailed);
}
function getAvengersComplete(response) {
return response.data.results;
}
function getAvengersFailed(error) {
logger.error('XHR Failed for getAvengers.' + error.data);
}
}
})();
Answer the question
In order to leave comments, you need to log in
You have some meat in general, to be honest : (
Usually separate the configuration for $resource and response processing into different parts of the code.
In your case, I would do this:
(function() {
'use strict';
angular
.module('LenaPillars')
.factory('ProfileResource', ProfileResource);
ProfileResource.$injector = ['$rootScope', '$resource'];
function ProfileResource($rootScope, $resource) {
var photosResource = $resource(
$rootScope.storeUrl,
{
checkActiveProfile: {
method: 'GET',
isArray: false,
url: $rootScope.storeUrl+'/checkProfile'
}
}
);
function checkActiveProfile() {
return new photosResource()
.$checkActiveProfile({token: $rootScope.token, setting_profiles: 'setting_profiles'})
// then принимает 2 параметра, для resolve и reject
.then(getAvengersComplete, getAvengersFailed);
function getAvengersComplete(response) {
return response.data.results;
}
function getAvengersFailed(error) {
logger.error('XHR Failed for getAvengers.' + error.data);
}
}
return {
checkActiveProfile: checkActiveProfile,
}
}
})();
function getProfileCheck() {
return ProfileResource
.checkActiveProfile()
.then(function(data) {
console.log('gde ti moi data: '+data);
vm.avengers = data;
return vm.avengers;
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question