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;
};