Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Angular calling service is failed! #1326


User avatar
0
zokho created

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)
  • User Avatar
    0
    finallyfreeguy created

    Try data.result instead of data.workTypes in success event.

  • User Avatar
    0
    zokho created

    that did not work!

  • User Avatar
    0
    komunamu created

    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?

  • User Avatar
    0
    ismcagdas created
    Support Team

    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.