There is an angularjs service that performs a POST http request and returns a response:

app.service('loginservice', function ($http) { this.login = function (userlogin) { var parameter = JSON.stringify({ Email: userlogin.username, Password: userlogin.password }); var resp = $http({ url: "http://localhost:62366/api/account/login", method: "POST", data: parameter, headers: { 'Content-Type': 'application/json' } }); return resp; }; }); 

The full text of Script'a, which is performed when you press the login button on one page and must go to another:

 (function () { 'use strict'; var app = angular.module('app'); app.controller('LoginController', function ($scope, loginservice, ) { //Scope Declaration $scope.responseData = ""; $scope.user = ""; $scope.userRegistrationEmail = ""; $scope.userRegistrationPassword = ""; $scope.userRegistrationConfirmPassword = ""; $scope.userLoginEmail = ""; $scope.userLoginPassword = ""; $scope.accessToken = ""; $scope.refreshToken = ""; //Ends Here //Function to Login. This will generate Token $scope.justlogin = function () { //This is the information to pass for token based authentication var userLogin = { grant_type: 'password', username: $scope.userLoginEmail, password: $scope.userLoginPassword }; var promiselogin = loginservice.login(userLogin); promiselogin.then(function (resp) { //Store the token information in the SessionStorage //So that it can be accessed for other views sessionStorage.setItem('userName', resp.data.role); sessionStorage.setItem('accessToken', resp.data.id_token); sessionStorage.setItem('refreshToken', resp.data.refresh_token); $scope.user = resp.data.role; console.log($scope.user); window.location.href = '/#/profile'; }, function (err) { $scope.responseData = "Error " + err.status; }); }; }); var app = angular.module('app'); app.service('loginservice', function ($http) { this.login = function (userlogin) { var parameter = JSON.stringify({ Email: userlogin.username, Password: userlogin.password }); var resp = $http({ url: "http://localhost:62366/api/account/login", method: "POST", data: parameter, headers: { 'Content-Type': 'application/json' } }); return resp; }; }); }()) 

In general, everything is fine and works, but the variable in the view I’ve left unchanged on the $ scope.user property, although the log shows the necessary changes to the console.

HTML page:

 <div class="row wrapper border-bottom white-bg page-heading"> <div class="col-lg-10" ng-controller="LoginController"> <h2>{{user}}</h2> 

I have been puzzling over this issue for more than a day and would be very grateful if someone could help solve such a puzzle ...

  • Comments are not intended for extended discussion; conversation moved to chat . - Nicolas Chabanovsky
  • Found a satisfactory "crutch". I just pull the parameter I need from sessionStorage and assign it to the necessary property: setTimeout(function () { $scope.$apply(function () { $scope.user = sessionStorage.getItem('userName'); }); }, 2000); - Akaky

1 answer 1

You can call $ scope. $ Apply () after updating $ scope.user