Problem with Controller and a Service

447 views
Skip to first unread message

mark goldin

unread,
Sep 8, 2014, 2:51:09 PM9/8/14
to ang...@googlegroups.com
Service:
var myModule = angular.module('myApp', []);

myModule.factory("AirportService", function ($http, $q) {

    return {
        getData : function(){
            var deferred = $q.defer();
            var response = $http.get("griddata.json");
            response.success(function (data) {
                deferred.resolve(data);
            });
            response.error(function (data) {
                alert('Error 123');
            });
            // Return the promise to the controller
            return deferred.promise;
        }
    }
});

Controller:
var myModule = angular.module("myApp", []);
myModule.controller('AirportControllers', [function ($scope, $http, AirportService) {
    AirportService.getData().then(function(data) {
        $scope.agents = data;
    });    
}]);

No matter what I do I get:
TypeError: Unable to get property 'getData' of undefined or null reference
 at Anonymous function (http://..../angular/controllers.js:3:5)

Any idea?

Thanks

Sander Elias

unread,
Sep 9, 2014, 12:30:14 AM9/9/14
to ang...@googlegroups.com

Hi Mark

You are redefining your module in stead of just getting a reference.

angular.module("myApp", []); defines a module
angular.module("myApp"); gets a reference to the same module. (notice the absentt ,[])

This is the root cause of your problem

Regards
Sander

mark goldin

unread,
Sep 9, 2014, 2:46:27 AM9/9/14
to ang...@googlegroups.com
Yes, that was a problem. Thanks


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/f6eUD4wxkrM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sudhakar Kota

unread,
Apr 8, 2015, 5:52:55 AM4/8/15
to ang...@googlegroups.com
Iam getting the  TypeError: Unable to get property 'then' of undefined or null reference when try to use the below code.

var app = angular.module(,$scope,['tableSort']);
   //Get All the Dash Board Services
   app.factory('ResourceDetailsFactory', function ($http) {
       return {
           GetResources: function (ResourceType, Status) {
               //since $http.get returns a promise,
               //and promise.then() also returns a promise
               //that resolves to whatever value is returned in it's 
               //callback argument, we can return that.
               $http.post('dummay.aspx/Getmethod', { resource: ResourceType, status: Status }).then(function (result) {
                             return result.data;
               });
           }
       }
   });
 ResourceDetailsFactory.GetResources(ResourceType, Status).then(function (data) {
         
        });

Any one can help me what wrong in the 
ResourceDetailsFactory.GetResources(ResourceType, Status).then(function (data) {}

Thanks
Sudhakar

----------------------------------------------------------------------------------------------------------------------------------
Reply all
Reply to author
Forward
0 new messages