3 rules
3 rules

Reputation: 1409

Angular js: How to Use constants values in other services without changing it

I am new to Angular js and want to set the global variable that I can use it on different different services as webservice url.

I have implemented app.js (module) like this:

var app;
(function () {
    app = angular.module("ANG", []);

    app.value('user', {
        urlBase: ''
    });
})();

When I want to use this constants values I just define it in the service defination and use it but I want to store the details in that so no need to set values again and again.

Example:

When I login in the system I return some data with webserviceurl with it and I set that url in the value.urlBase in login controller.

But when I go to different service and want to use that it sets the values back to ''.

CODE to use constant values:

app.service('servicename', ["$http", "$q", 'user', function ($http, $q, user) {
//access the user here but it gives value ''.
console.log(user.urlBase);
}

How can I store at once and use it again and again?

Upvotes: 1

Views: 1035

Answers (3)

Jono Stewart
Jono Stewart

Reputation: 356

I suggest you have another service, rather than a value.

Pass a userService to your services, that contains the user object that is either accessible as a property, or retrieved via a getter:

app.factory('userService', function() {

    var user = {
        urlBase: ''
    };

    return {
        user: user
    };
};

app.service('servicename', ['userService', function (userService) {
    console.log(userService.user.urlBase);
}]);

Then where you set the urlBase, you are also passing the userService. Services are generally singletons, so it should preserve the user between other services.

UPDATE:

As you need to preserve the details between page refreshes, a cookie is not a bad solution. In your loginService, you set the cookie value, and in your other services, read from the cookie. This is done using the $cookies service in Angular (https://docs.angularjs.org/api/ngCookies/service/$cookies)

app.service('loginService', ['$cookie', function ($cookies) {
    $cookies.put('userUrl', 'http://urlhere');
}]);

app.service('servicename', ['$cookie', function ($cookies) {
    console.log($cookies.get('userUrl'));
}]);

Another option is to use local storage, and this SO has an example of doing so and preserving state:

How do I store data in local storage using Angularjs?

Upvotes: 1

Jaimin Raval
Jaimin Raval

Reputation: 156

You can use constant files like this.

app = angular.module("ANG", []);
app.constant('user', {
    urlBase: {
          'url':'what ever the URL is'
     }
});

Upvotes: 0

rrd
rrd

Reputation: 5957

You can actually use a 'constants' file, like so:

export default angular
  .module('my.module', [])
  .constant('someConstant', Object.freeze({
    foo: 1,
    bar: 2
  })
  .name;

We use Object.freeze() in ours, but you can use immutable.js or something like that to prevent mutation.

Upvotes: 1

Related Questions