Answer the question
In order to leave comments, you need to log in
How to change a variable asynchronously in angular?
I usually did this:
var app = angular.module('myApp', []).controller('myCtrl', ['$scope', function($scope) {
$scope.authorizedApp = false;
$scope.example = function () {
$scope.authorizedApp = true; //так меняла значения переменных
}
}]);
Answer the question
In order to leave comments, you need to log in
For asynchronous requests, you should use the so-called promises (promice), in angular they are in the $q library
From the obvious solutions to me, don't use global variables. At you one value is dragged on inheritance in all controlers?
The $q library is really what you need.
It is done like this:
$scope.example = function (value) {
var q = $q.defer();
if (value == 'what_you_need') {
q.resolve(variable); //подставляете в variable значение, которое вам надо
}
else {
q.reject(variable); //подставляете в variable значение, которое вам надо в противных случаях
}
return q.promise;
}
$scope.auth = false; //эта переменная и примет значение, когда callback отработает
$scope.promise = $scope.example(your_variable);
$scope.promise.then ( function (v) { $scope.auth = v },
function (err) { $scope.auth = err }
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question