AngularJS: Error: $q is not defined

Metaphysiker picture Metaphysiker · Dec 17, 2016 · Viewed 8.4k times · Source

I want to make a promise in my angularjs controller. I took the example from the Angularjs Doc and pasted it in my controller. When I try to run the code, the console prints:

Error: $q is not defined

Why is this error happening and how do I solve it?

I tried to google this problem, but most questions revolve about more specific problems than mine.

A (german) guide tells me that promises are already in angular js implemented and there is no need to add anything to it.

EDIT:

this is my controller:

app.controller("ArgumentationController", [
    '$scope', '$resource',
    function($scope, $resource) {

EDIT2: A commentor suggested to inject $q. I did this:

app.controller("ArgumentationController", [
    '$scope', '$resource', '$q',
    function($scope, $resource, $q) {

Now, the error does not occur.

Answer

Sai picture Sai · Dec 17, 2016

From your past code, no need of $resource in your code. Instead inject $q in it. As you are creating a dummy promise using $q, make following changes.

 app.controller("ArgumentationController", [
        '$scope', '$q',
        function($scope, $q) {