0

I have this in my AppCtrl.js:

$scope.facebookUserDone = $q.defer();

and I would like to access this variable from a view's controller :

WeightLevel.js:

$q.when($scope.facebookUserDone.promise).then(function () {
                $scope.userFaceUrl = userr.pictureUrl;
});

Is that possible or should I put the variable on the $rootScope ?

3 Answers 3

2

Create a factory to store the variable in and then inject this factory into both controllers.

Factory

angular
    .module('app')
    .factory('dataservice', dataservice);

function dataservice($http, logger) {
    var dataStore = {};
    return {
        setVariable: setVariable,
        getVariable: getVariable
    };

    function setVariable(name,value) {
        dataStore[name] = value;
    }

    function getVariable(name){
       return dataStore[name]
    }
}

Controllers

angular.module('app').controller('MyCtrl', myCtrl);

myCtrl.$inject = ['$scope','dataservice'];

function myCtrl($scope,dataservice){
    dataservice.setVariable('foo','bar');
    $scope.foo = dataservice.getVariable('foo');
}

Using the dataservice you can now setVariable and store this to the factory and access this from any controller using the dataservice.getVariable method.

Sign up to request clarification or add additional context in comments.

6 Comments

Did you notice it is deferred variable ($q) ? Will I be able to resolve and reject it easily like this ?
@Louis ah I did not no, best bet is to save the variable to a factory then when it's completed broadcast an event I think.
ok thanks. Seems complicated ! Isn't it a simpler solution to put it on the rootScope ?
Probably yeah then once you've completed the promise remove it from the $rootScope with delete $rootScope.yourPromiseName
ok I see, is it ok to $broadcast an event from the AppCtrl when the promise is resolved and to $on in the view controller to get the signal ?
|
0

Create a Service and Inject in both Controllers.

Comments

0

Just to offer some other solutions. You can use either values or events (depends on your setup as you might have some concurrency issues).

The following example describes the usage of angular values in your case and assumes your module is called "app".

angular
    .module('app')
    .value('states', {
        facebookUser: null
    });

Then in your AppCtrl, you can inject the "states" value set it to a deferred promise:

angular
    .module('app')
    .controller('AppCtrl', ($q, states) => {
        states.facebookUser = $q.defer();
    });

This way you can inject and access the value in your other controllers.

Using values might be great if you just need some kind of global state. If you need something more robust, using service or factory might be more appropriate.

Comments

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.