Re: [AngularJS] 'defer' undefined when using $q in controller

7,556 views
Skip to first unread message
Message has been deleted

Chris Rhoden

unread,
May 15, 2014, 11:51:25 AM5/15/14
to ang...@googlegroups.com
You aren't injecting $q, because your injection annotation does not include it.


On Thu, May 15, 2014 at 11:46 AM, Chris <squirr...@gmail.com> wrote:
hi all,

i'm new to angularjs, trying to use $q in a controller with no luck.  any ideas?  
thanks

TypeError: Cannot read property 'defer' of undefined


Application.Controllers.controller("XXX", ['$scope'], function($scope, $q) {

$scope.myFunc = function(){
var defer = $q.defer(),
myPromise;
 
myPromise = $scope.getData()
.then($scope.getData2)
.then(function(data) {
defer.resolve(); // resolve our defer
        });
// return our promise
return defer.promise();
}
});

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, 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.



--
chrisrhoden

Chris Rhoden

unread,
May 15, 2014, 11:52:23 AM5/15/14
to ang...@googlegroups.com
Also, here's a hint. It isn't saying that defer is undefined, it's saying that you're trying to read the `defer` property of undefined.
--
chrisrhoden
Message has been deleted

Chris Rhoden

unread,
May 15, 2014, 7:11:32 PM5/15/14
to ang...@googlegroups.com
nothing but `angular` is global.

To make your thing work you need to do:

Application.Controllers.controller("XXX", ['$scope', '$q', function($scope, $q) {

...

}]);

Note that this is not an endorsement of the apparent structure of your application.

Typically, I chain all definitions off of the module object instead of making them global, as in:

angular.module('module.name', ['module.dependency'])
.controller('ControllerName', ['$scope', '$q', function ($scope, $q) {
 ...
}])
.factory( ... );


On Thu, May 15, 2014 at 12:00 PM, Chris <squirr...@gmail.com> wrote:
I guess I don't understand where $q comes from.  I thought you would have full access just like $ if you in jQuery.  
Even if I do something like this, it has to be defined somewhere else?

Application.Controllers.controller("XXX", ['$scope', '$q'], function($scope, $q) {



--
chrisrhoden

Chris

unread,
May 16, 2014, 10:47:32 AM5/16/14
to ang...@googlegroups.com
Thank you for the help.  Unfortunately I'm learning as I go, working on a large app that is already built, so I can't make big changes (yet).
Reply all
Reply to author
Forward
0 new messages