-3

I'm trying to understand why some of the dependecies injected (or other variables declared) in my controller are not available inside the success/error callback of the 'then' function of a deferred promise.

I've searched in angular $q's documentation and in several posts here but I'm not being able to answer my question... Maybe I'm not searching the right keywords, I don't know.

Here's a snippet of my controller with the questions that I want to know. Thanks in advance!

(function () {
'use strict';
angular
    .module('app.aModule')
    .controller('mycontroller', MyController);

MyController.$inject = ['$controller', '$scope', 'myService'];

function MyController($controller, $scope, myService) {

    $scope.myProp;
    var myVariable1 = 'hello';

    //.............some code.................

    $scope.save = function(event) {
            var myVariable2 = 'World!';
            myService.post($scope.myProp).then(
                function (result) {
                    // 1. can I access to myVariable1 here?
                    // 2. can I access to myVariable2 here?
                    // 3. can I access to $scope or $controller dependency here?
                    // 4. can I access to myService dependency here?
                },
                function (error) {
                    // do something
                }
            );
        };

    //.............some code.................
}

})();

And here is the post method of my angular service:

 function post(data) {
        var deferred;
        deferred = $q.defer();

        $http.post(apiUrl + endpoint + 'post', data).then(function (result) {
            deferred.resolve(result);
        }, function (error) {
            deferred.reject(error);
        });

        return deferred.promise;
    };
DiegoA29
  • 1
  • 2
  • Just debug. It is easy – Mediator Apr 27 '16 at 01:59
  • @Mediator Hehe I've already did it but I would like to understand the theory behind. BTW, when debugging I only could access to $scope and myService dependencies (not to $controller and the other variables). – DiegoA29 Apr 27 '16 at 02:04
  • 1-3 answer is yes, not sure what you are refering to in 4th point, `$http.post` already returns promise, stop wrapping it with `deferred.promise` ... – mido Apr 27 '16 at 03:16
  • Hi @mido! In fact, as I mentioned in my previous comment, I can only access to $scope and myService (an angular service that I've created and injected in the controller as you may see). I cannot access to the variables 1 and 2, and neither to the $controller – DiegoA29 Apr 27 '16 at 03:34

1 Answers1

0

It is not depends with promises. You can't understand closure function.

A closure is a function having access to the parent scope(it is not angular scope), even after the parent function has closed. If you write myVariable1 or(and) myVariable2 into function it makes it possible to see this varibales, but if you just debug it you can't see it(if you didn't write it).

Answers: 1 and 2 - depends how you do it. 3. $scope - yes. controller - can't understand what is mean. 4. same answer as 1 and 2.

Mediator
  • 14,951
  • 35
  • 113
  • 191
  • I know what a closure is. I DID write the code so I DID debug it, and the result of the debugging is the one a answered to your previous non-useful comment. Can you explain more about your answers regarding "depends how you do it"? It's a bit generic. $controller service is responsible for instantiating controllers.... but you should read more about angular if you can't understand this kind of things. – DiegoA29 Apr 27 '16 at 12:29
  • What you explained about turning the variables into functions doesn't make sense. If you understand what "parent scope" means you'd know that it's not a matter of being functions or variables. – DiegoA29 Apr 27 '16 at 12:39
  • I suggest create jsfiddle. Because you have to see myVariable1 and myVariable2. When I told about "depends how you do it" I mean you HAVE TO write myVariable1 inside promise. IF you don't write you will not see it in debug. – Mediator Apr 27 '16 at 13:03