简体   繁体   中英

Call a function inside an object in AngularJS factory

angular.module('snswMock').factory('snswService', ['$http','$location' ,
    function ($http, $location ) {

        'use strict';

        return {

            getData: function(jsonData){

                var jsonString = JSON.stringify(jsonData);
                var serviceUrl = getServiceUrl();
                $http({
                        url: serviceUrl,
                        dataType: 'string',
                        method: 'POST',
                        data: jsonString
                    }).success(function(response){
                        alert ("Success");
                    }).error(function(error){
                        alert ("Save company!");
                    });

      },

        getServiceUrl :function(){

                    var host = $location.host();
                    if (host === 'localhost') {
                        return $location.protocol() + '://' + host + ':9000/services/insert';
                    } else {
                        return $location.protocol() + '://' + host + '/services/insert';
                    }

            }
        }
    }
]);

Hi am very new to the angular this is my service

I am calling getServiceUrl inside getData function i am getting the below error

angular.js:11706 ReferenceError: getServiceUrl is not defined

can anyone please help me how can I call the method. is there any other way is there to call web service by passing a string as post request?

You need to use this context to access a global object function -> this.getServiceUrl() . In your case myFactory is the object and inside your object all functions are globally accessible inside your object scope by using this context.

angular.module('snswMock').factory('snswService', ['$http', '$location', function($http, $location) {

  'use strict';

  var myFactory = {

    getData: function(jsonData) {

      var jsonString = JSON.stringify(jsonData);
      var serviceUrl = this.getServiceUrl(); //modified

      $http({
        url: serviceUrl,
        dataType: 'string',
        method: 'POST',
        data: jsonString
      }).success(function(response) {
        alert("Success");
      }).error(function(error) {
        alert("Save company!");
      });

    },

    getServiceUrl: function() {

      var host = $location.host();
      if (host === 'localhost') {
        return $location.protocol() + '://' + host + ':9000/services/insert';
      } else {
        return $location.protocol() + '://' + host + '/services/insert';
      }

    }
  }

  return myFactory;
}]);

The controller where you are trying to access this service, do below things.

  1. Inject this service in the controller.

  2. Call this service like below

     snswService.getServiceUrl(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM