1

I want to use the variable between functions in one controller. Controller: app.controller { .......

$http.get("json.php")
.success(function (response) {
    $scope.m = response.p;
    $scope.licznikm = $scope.m.length;
    $scope.date = new Date();
    $scope.openp = function (id, m) {
        ngDialog.open({
            template : 'formn.php',
            className : 'ngdialog-theme-default',
            backdrop : 'static',
        });
        $scope.name = m;
        $scope.id = id;
        console.log($scope.name); //is OK
        console.log($scope.id); // is OK
    };
    console.log($scope.miasta); //undefined
    console.log($scope.id); //undefined
1
  • 1
    As a side note .success is deprecated. You should just use .then and .catch for error handling. Commented Jan 26, 2016 at 9:02

2 Answers 2

2

the http call is asynchronous meaning the final logging code below will probably run before the http call has completed and the values have been set. Simplest solution is to ensure you have the values before using them, this can be done by wrapping the code that uses them in a callback called after the http success. e.g.

$http.get("json.php")
.success(function (response) {
    $scope.m = response.p;
    $scope.licznikm = $scope.m.length;  
    $scope.date = new Date();  
    $scope.openp = function (id,m) {
        ngDialog.open({ 
            template: 'formn.php',
            className: 'ngdialog-theme-default',
            backdrop : 'static',
        });
        $scope.name = m;
        $scope.id = id;
        console.log($scope.name); //is OK
        console.log($scope.id);   // is OK
       // a callback fn
        useNewValues();
    };
});

function useNewValues() {
    // should be the vals from http response now
    console.log($scope.name);
    console.log($scope.id);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I would like to appeal to these variables globally example, in the view {{}}
is the view controlled by this controller? if so then perhaps you should wrap the assigment into the digest e.g. $scope.$apply(function() { $scope.name = m; $scope.id = id; }); which will ensure the view is updated with the new vals once fetched, your view should have access to $scope vals but when you update them the view will not necessarily refresh
There is no need for $scope.$apply if the $http service is used. It may throw an exception is a digest cycle is already running. $apply is only needed when an action is used out of angulars scope, say a jQuery event. In this case the view should just update when the value from the server returns. If it isn't it points to an issue elsewhere.
0

You can save a promise and use it at any time to ensure code doesn't run until it has completed.

var have_data = $http.get("json.php")
.then(function (response) {
    $scope.m = response.p;
    $scope.licznikm = $scope.m.length;
    $scope.date = new Date();
    $scope.openp = function (id, m) {
        ngDialog.open({
            template : 'formn.php',
            className : 'ngdialog-theme-default',
            backdrop : 'static',
        });
        $scope.name = m;
        $scope.id = id;
        console.log($scope.name); //is OK
        console.log($scope.id); // is OK
    });
 });

Then when you want to do anything else that requires you have actually retrieved the data just wrap it with the promise:

have_data.then(function() {
    console.log($scope.id);
});

If the data has been fetched the function will execute almost immediately, if it hasn't yet arrived it will execute as soon as data does arrive.

You can re-use the have_data promise this way as many times as you need.

2 Comments

I would like to use $scope.id globally in the controller so that other functions could use it as a parameter
Just move all of the code that references it inside the have_data.then function. Anything outside it will execute before the data arrives. From inside that callback you can safely use it as a parameter to call other functions. From outside it you can check it for undefined, or in a view you can wrap your html with ng-if but it won't be set until after your controller body has finished executing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.