Skip to main content
edited body
Source Link
bingjie2680
  • 7.8k
  • 9
  • 50
  • 72

Angular doc states:

Angular services are singletons

I want to use the angular service as singleton, so I can access the logged-in user data every where in my application. but the serivce does not seem to return the same data, here is my codes.

Service:

angular.module("myapp", [])

.service("identity", function (){
    this.token = null;
    this.user = null;
});

Facotry:

.factory("authentication", function (identity, config, $http, $cookieStore) {
var authentication = {};

authentication.login =  function (email, password, remember) {
    var p=$http.post(config.baseUrl+"api/","email="+email+"&password="+password);

    return p.then(function (response) {
            identity= response.data;

            if (remember) {
                $cookieStore.put("identity", identity);
            }
        });
};

authentication.isAuthenticated = function () {
    if (!identity.token) {
        //try the cookie
        identity = $cookieStore.get("identity") || {};
    }
    console.log(identity) // get{token: identity23832943, datauser: {name: something}}
    return !!identity.token;
};

return authentication;
});

controller:

.controller('LoginCtrl', function ($state, $scope, authentication, identity) {

    var user = $scope.user = {};

    $scope.login = function () {
        authentication.login(user.email, user.password, user.remember)
        .then(function () {
            if (authentication.isAuthenticated()) {
                console.log(identity); // does not get{token:null, identityuser: datanull}
                $state.transitionTo("dashboard");
            }
        });
    };
});

The identity is injected to both Authenticationauthentication and controller. But the first console logs the correct user data, while the second console just returnlogs the same data as initially defined. If the service is singleton as stated, I would expect two identity returns the same data, because the two are called nearly next to each other. What am I doing wrong here?. any pointers are appreciated.

Angular doc states:

Angular services are singletons

I want to use the angular service as singleton, so I can access the logged-in user data every where in my application. but the serivce does not seem to return the same data, here is my codes.

Service:

angular.module("myapp", [])

.service("identity", function (){
    this.token = null;
    this.user = null;
});

Facotry:

.factory("authentication", function (identity, config, $http, $cookieStore) {
var authentication = {};

authentication.login =  function (email, password, remember) {
    var p=$http.post(config.baseUrl+"api/","email="+email+"&password="+password);

    return p.then(function (response) {
            identity= response.data;

            if (remember) {
                $cookieStore.put("identity", identity);
            }
        });
};

authentication.isAuthenticated = function () {
    if (!identity.token) {
        //try the cookie
        identity = $cookieStore.get("identity") || {};
    }
    console.log(identity) // get identity data
    return !!identity.token;
};

return authentication;
});

controller:

.controller('LoginCtrl', function ($state, $scope, authentication, identity) {

    var user = $scope.user = {};

    $scope.login = function () {
        authentication.login(user.email, user.password, user.remember)
        .then(function () {
            if (authentication.isAuthenticated()) {
                console.log(identity); // does not get identity data
                $state.transitionTo("dashboard");
            }
        });
    };
});

The identity is injected to both Authentication and controller. But the first console logs the correct user data, while the second console just return the same data as initially defined. If the service is singleton, I would expect two identity returns the same data, because the two are called nearly next to each other. What am I doing wrong here?. any pointers are appreciated.

Angular doc states:

Angular services are singletons

I want to use the angular service as singleton, so I can access the logged-in user data every where in my application. but the serivce does not seem to return the same data, here is my codes.

Service:

angular.module("myapp", [])

.service("identity", function (){
    this.token = null;
    this.user = null;
});

Facotry:

.factory("authentication", function (identity, config, $http, $cookieStore) {
var authentication = {};

authentication.login =  function (email, password, remember) {
    var p=$http.post(config.baseUrl+"api/","email="+email+"&password="+password);

    return p.then(function (response) {
            identity= response.data;

            if (remember) {
                $cookieStore.put("identity", identity);
            }
        });
};

authentication.isAuthenticated = function () {
    if (!identity.token) {
        //try the cookie
        identity = $cookieStore.get("identity") || {};
    }
    console.log(identity) // {token: 23832943, user: {name: something}}
    return !!identity.token;
};

return authentication;
});

controller:

.controller('LoginCtrl', function ($state, $scope, authentication, identity) {

    var user = $scope.user = {};

    $scope.login = function () {
        authentication.login(user.email, user.password, user.remember)
        .then(function () {
            if (authentication.isAuthenticated()) {
                console.log(identity); // {token:null, user: null}
                $state.transitionTo("dashboard");
            }
        });
    };
});

The identity is injected to both authentication and controller. But the first console logs the correct user data, while the second console just logs the same data as initially defined. If the service is singleton as stated, I would expect two identity returns the same data. What am I doing wrong here?. any pointers are appreciated.

added 65 characters in body
Source Link
bingjie2680
  • 7.8k
  • 9
  • 50
  • 72

Angular doc states:

Angular services are singletons

I want to use the angular service as singleton, so I can access the logged-in user data every where in my application. but the serivce does not seem to return the same data, here is my codes.

Service:

angular.module("myapp", [])

.service("identity", function (){
    this.token = null;
    this.user = null;
});

Facotry:

.factory("authentication", function (identity, appConfigconfig, $http, $cookieStore) {
var authentication = {};

authentication.login =  function (email, password, remember) {
    var p = $httpp=$http.post(appConfigconfig.baseUrl + "apibaseUrl+"api/", "username=" + email + "&password=" + password"email="+email+"&password="+password);

    return p.then(function (response) {
            identity= response.data;

            if (remember) {
                $cookieStore.put("identity", identity);
            }
        });
};

authentication.isAuthenticated = function () {
    if (!identity.token) {
        //try the cookie
        identity = $cookieStore.get("identity") || {};
    }
    console.log(identity) // get identity data
    return !!identity.token;
};

return authentication;
});

controller:

.controller('LoginCtrl', function ($state, $scope, authentication, identity) {

    var user = $scope.user = {};

    $scope.login = function () {
        authentication.login(user.email, user.password, user.remember)
        .then(function () {
            if (authentication.isAuthenticated()) {
                console.log(identity); // does not get identity data
                $state.transitionTo("dashboard");
            }
        });
    };
});

SoThe identity is injected to both Authentication and controller. But the first console logs the correct user data, while the second console just return the same data as initially defined. If the service is singleton, I would expect two identity returns the same data, because the two are called nearly next to each other. What am I doing wrong here?. any pointers are appreciated.

Angular doc states:

Angular services are singletons

I want to use the angular service as singleton, so I can access the logged-in user data every where in my application. but the serivce does not seem to return the same data, here is my codes.

Service:

angular.module("myapp", [])

.service("identity", function (){
    this.token = null;
    this.user = null;
});

Facotry:

.factory("authentication", function (identity, appConfig, $http, $cookieStore) {
var authentication = {};

authentication.login =  function (email, password, remember) {
    var p = $http.post(appConfig.baseUrl + "api/", "username=" + email + "&password=" + password);

    return p.then(function (response) {
            identity= response.data;

            if (remember) {
                $cookieStore.put("identity", identity);
            }
        });
};

authentication.isAuthenticated = function () {
    if (!identity.token) {
        //try the cookie
        identity = $cookieStore.get("identity") || {};
    }
    console.log(identity) // get identity data
    return !!identity.token;
};

return authentication;
});

controller:

.controller('LoginCtrl', function ($state, $scope, authentication, identity) {

    var user = $scope.user = {};

    $scope.login = function () {
        authentication.login(user.email, user.password, user.remember)
        .then(function () {
            if (authentication.isAuthenticated()) {
                console.log(identity); // does not get identity data
                $state.transitionTo("dashboard");
            }
        });
    };
});

So the first console logs the correct user data, while the second console just return the same data as initially defined. If the service is singleton, I would expect two identity returns the same data, because the two are called nearly next to each other. What am I doing wrong here?. any pointers are appreciated.

Angular doc states:

Angular services are singletons

I want to use the angular service as singleton, so I can access the logged-in user data every where in my application. but the serivce does not seem to return the same data, here is my codes.

Service:

angular.module("myapp", [])

.service("identity", function (){
    this.token = null;
    this.user = null;
});

Facotry:

.factory("authentication", function (identity, config, $http, $cookieStore) {
var authentication = {};

authentication.login =  function (email, password, remember) {
    var p=$http.post(config.baseUrl+"api/","email="+email+"&password="+password);

    return p.then(function (response) {
            identity= response.data;

            if (remember) {
                $cookieStore.put("identity", identity);
            }
        });
};

authentication.isAuthenticated = function () {
    if (!identity.token) {
        //try the cookie
        identity = $cookieStore.get("identity") || {};
    }
    console.log(identity) // get identity data
    return !!identity.token;
};

return authentication;
});

controller:

.controller('LoginCtrl', function ($state, $scope, authentication, identity) {

    var user = $scope.user = {};

    $scope.login = function () {
        authentication.login(user.email, user.password, user.remember)
        .then(function () {
            if (authentication.isAuthenticated()) {
                console.log(identity); // does not get identity data
                $state.transitionTo("dashboard");
            }
        });
    };
});

The identity is injected to both Authentication and controller. But the first console logs the correct user data, while the second console just return the same data as initially defined. If the service is singleton, I would expect two identity returns the same data, because the two are called nearly next to each other. What am I doing wrong here?. any pointers are appreciated.

Source Link
bingjie2680
  • 7.8k
  • 9
  • 50
  • 72

angular service not work as singleton

Angular doc states:

Angular services are singletons

I want to use the angular service as singleton, so I can access the logged-in user data every where in my application. but the serivce does not seem to return the same data, here is my codes.

Service:

angular.module("myapp", [])

.service("identity", function (){
    this.token = null;
    this.user = null;
});

Facotry:

.factory("authentication", function (identity, appConfig, $http, $cookieStore) {
var authentication = {};

authentication.login =  function (email, password, remember) {
    var p = $http.post(appConfig.baseUrl + "api/", "username=" + email + "&password=" + password);

    return p.then(function (response) {
            identity= response.data;

            if (remember) {
                $cookieStore.put("identity", identity);
            }
        });
};

authentication.isAuthenticated = function () {
    if (!identity.token) {
        //try the cookie
        identity = $cookieStore.get("identity") || {};
    }
    console.log(identity) // get identity data
    return !!identity.token;
};

return authentication;
});

controller:

.controller('LoginCtrl', function ($state, $scope, authentication, identity) {

    var user = $scope.user = {};

    $scope.login = function () {
        authentication.login(user.email, user.password, user.remember)
        .then(function () {
            if (authentication.isAuthenticated()) {
                console.log(identity); // does not get identity data
                $state.transitionTo("dashboard");
            }
        });
    };
});

So the first console logs the correct user data, while the second console just return the same data as initially defined. If the service is singleton, I would expect two identity returns the same data, because the two are called nearly next to each other. What am I doing wrong here?. any pointers are appreciated.