2016-08-30 2 views
1

서비스에 정의 된 함수를 호출하려고했습니다.오류 : 기능 없음 - 각도 서비스

var app = angular.module('title', ['flash', 'ngAnimate', 'ngRoute'], 
    function ($interpolateProvider) { 

     $interpolateProvider.startSymbol('[['); 
     $interpolateProvider.endSymbol(']]'); 
    }) 

.service('getWidgets', function (globalServices, $http) { 

    var getData = function() { 

     var getWidgetUrl = globalServices.baseUrl + "admin/widget/list-text-widget"; 
     return $http({method:"GET", url:getWidgetUrl}) 
      .then(function(result){ 

       return result.data; 
      }); 
    }; 

    return { getData: getData }; 
}); 

섹션

var widget = getWidgets.getData() 
    .then(function (result) { 

     $scope.widgets = result; 
     $scope.$apply(); 
    }); 

호출하지만 오류 getWidgets.getData is not a function를 반환합니다.

근본 원인은 무엇입니까?

+3

하면 전체 코드를 게시 할 수 있습니다 : 당신이 조언을 원하는 경우,이 구문을 사용합니까? angular.module ('...'). service (....) – hpfs

+0

전체 코드가 추가되었습니다. 지금 확인하십시오. – Girish

+0

Controller ('widgetCtrl', [ '$ scope', '$ compile', '$ window', '')는 컨트롤러의 정의를 게시 할 수 있습니다 (이 서비스를 사용하는 곳에서) – gianlucatursi

답변

2

변경을 시도

widgetCtrl.$inject = ['$scope', '$compile', '$window', '$location', '$http', 'globalServices', 'getWidgets', 'Flash', '$timeout', '$sce', '$routeParams']; 

angular.module('dss').controller('widgetCtrl', widgetCtrl); 

function widgetCtrl($scope, $compile, $window, $location, $http, globalServices, getWidgets, Flash, $timeout, $sce, $routeParams) { 

    var widget = getWidgets.getData(); 
    widget.then( 
     function (result) { 
      $scope.widgets = result; $scope.$apply(); 
     });  
} 
+0

고마워요. – Girish

0

서비스를 사용하고 해당 생성자에서 개체를 반환하고 있습니다. 서비스는 new yourFunction으로, 공장은 yourFunction()으로 초기화됩니다.

서비스에서 서비스로 전환하면 올바르게 작동합니다.

EDIT : 서비스를 계속 사용하려면이 기능을 사용해보십시오. 참고 나는 서비스의 이름을 변경

function GetWidgetsService($http, globalServices){ 
    this._$http = $http; 
    this._globalServices = globalServices; 
} 

GetWidgetsService.prototype.getData = function() { 
    var getWidgetUrl = this._globalServices.baseUrl + "admin/widget/list-text-widget"; 
    // Angular $http() and then() both return promises themselves 
    return this._$http({method:"GET", url:getWidgetUrl}).then(function(result){ 

     // What we return here is the data that will be accessible 
     // to us after the promise resolves 
     return result.data; 
    }); 
}; 

angular.module('yourModule').service('getWidgetsService', GetWidgetsService); 

편집 2 : HERE이 코드는 작업으로 JSBin이다 완성도를 들어, 여기 고정 공장

angular.module('yourModule').factory('getWidgetsFactory', function ($http, globalServices) { 
    return { 
     getData: function() { 
      var getWidgetUrl = globalServices.baseUrl + 'admin/widget/list-text-widget'; 
      // Angular $http() and then() both return promises themselves 
      return $http({method: 'GET', url: getWidgetUrl}).then(function (result) { 

       // What we return here is the data that will be accessible 
       // to us after the promise resolves 
       return result.data; 
      }); 
     } 
    }; 
}); 

EDIT 3입니다 내 첫 번째 솔루션.

angular.module('dss') 
    .controller('widgetCtrl', 
    ['$scope', '$compile', '$window', '$location', '$http', 'globalServices', 'getWidgets', 'Flash', '$timeout', '$sce', '$routeParams', widgetCtrl]); 

    function widgetCtrl($scope, $compile, $window, $location, $http, globalServices, getWidgets, Flash, $timeout, $sce, $routeParams) { 

    var widget = getWidgets.getData(); 
    widget.then(
     function (result) { 
      $scope.widgets = result; $scope.$apply(); 
     });  
} 

편집 :

+0

제가 이것을 시도하겠습니다. – Girish

+0

@Girish updated answer –

+0

같은 문제가 있습니다. 나는 그 생성자를 돌려 주려고하지 않고있다 – Girish

-1

이 방법이와

.service('getWidgets', function (globalServices, $http) { 
    return { getData: function() { 
     var getWidgetUrl = globalServices.baseUrl + "admin/widget/list-text-widget"; 
     // Angular $http() and then() both return promises themselves 
     return $http({method:"GET", url:getWidgetUrl}).then(function(result){ 

      // What we return here is the data that will be accessible 
      // to us after the promise resolves 
      return result.data; 
     }); 
    }; 
}; 
});