Girish Girish - 1 year ago 181
Javascript Question

Error: Not a function - Angular Service

I tried to call function defined in a service.

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


.service('getWidgets', function (globalServices, $http) {
var 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 { getData: getData };

Calling section

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

But it return an error
getWidgets.getData is not a function

What would be the root cause?

Answer Source

Change with this:

    ['$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(); 
      function (result) { 
          $scope.widgets = result; $scope.$apply(); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download