Hi, I have got a service in Angular as below:
(function () {
angular.module('app').factory('workType', ['abp.services.app.workType',
function (workTypeService) {
var workType = this;
workType.loadWorkTypes = function () {
abp.ui.setBusy(
null,
workTypeService.getWorkTypeList()
.success(function (data)
return data.workTypes;
})
.error(function (message) {
abp.notify.error(message);
})
);
};
return workType;
}
]);
})();
and a controller as
(function () {
var controllerId = 'app.views.configuration.advertisementFields';
angular.module('app').controller(controllerId, ['workType',
function (workTypeService) {
var vm = this;
vm.loadWorkTypes = function () {
var data = workTypeService.loadWorkTypes();
}
}
]);
})();
Here is my web API code:
public GetWorkTypeListOutput GetWorkTypeList()
{
var workTypes = _workTypeRepository.GetAll().ToList();
var workTypeList = new GetWorkTypeListOutput()
{
WorkTypes = Mapper.Map<List<WorkTypeDto>>(workTypes)
};
return workTypeList;
}
When I call the web api through the angular service defined above, the returned data is undefined! I can see that it hit the web API though. I am pretty sure I am making a mistake in line below within the controller, but do not know how to fix it:
var data = workTypeService.loadWorkTypes();
If I call the Web API directly from the controller it will work fine. but I need to call it through a service/factory.
4 Answer(s)
-
0
Try data.result instead of data.workTypes in success event.
-
0
that did not work!
-
0
Hello zokho,
I have the same problem. API call is working from Angular Controller but it does not work from Angular Service. Did you solve the problem?
-
0
Hi,
You need to design your service method something like in this example. <a class="postlink" href="https://docs.angularjs.org/api/ng/service/$q">https://docs.angularjs.org/api/ng/service/$q</a>
You cannot use return value directly like that.