L
L
leo_fr2016-01-18 17:53:50
JavaScript
leo_fr, 2016-01-18 17:53:50

How to correctly pass data in angularjs from controller to controller?

Good time of the day. I can not fully understand how to transfer data, namely the transfer of data from the controller to the controller.
1. authorization form 840c8ef328614173ba48378fa551fcb2.png
Login controller :

'use strict';

angular.module('prApp')
    .controller('loginCtrl',
    ['$scope','$rootScope','$location', 'loginService',
    function ($scope,$rootScope, $location,loginService) {
        loginService.ClearCredentials();
        $scope.login = function(){
            $scope.dataLoading = true;
            loginService.Login($scope.username, $scope.password, function(response){
                if(response.success){
                    loginService.SetCredentials($scope.username, $scope.password);
                    $location.path('/user/{{user.userId}}');
                    console.log('sd');
                    console.log($scope.userId);
                    console.log($scope.username, $scope.password);
                    console.log($scope)
                } else {
                    $scope.error = response.message;
                    $scope.dataLoading = false;
                }
            });
        };
    }]);

login service (code part)
'use strict';

angular.module('prApp')
    .factory('loginService',
    ['Base64','$http', '$cookieStore','$rootScope',
    function (Base64,$http, $cookieStore,$rootScope) {
        var service={};
        service.Login=function(username, password, callback){
            $http.post('/auth/log', { username: username, password: password })
                .success(function (response) {
                    callback(response);
                })
                .error(function (response) {
                    callback(response);
                });
        };

On the back-end of symfony (if success returns)
if ($user) {
                return new JsonResponse([
                    'username' => $user -> getUserName(),
                    'userId' => $user -> getId(),
                    'success' => true
                ]);

Next, we enter the data and the server returns the data to us
6c256214a4e34dfeb88fd90da14e90cd.png. And then the problems begin:
On the front-end I use $routeProvider routing
'use strict';

angular
    .module('prApp', [
        'ngAnimate',
        'ngCookies',
        'ngResource',
        'ngRoute',
        'ngSanitize',
        'ui.bootstrap',
        'ngTouch'
    ])
    .config(function ($routeProvider, $locationProvider)  {

        $routeProvider

            .when('/home', {
                templateUrl: 'app/views/home.html'
            })
            .when('/login', {
                templateUrl: 'app/views/login.html',
                controller: 'loginCtrl'
            })
  
            .when('/user/:userId', {
                templateUrl: 'app/views/user.html',
                controller: 'usersCtrl'
            })


            .otherwise({
                redirectTo: '/home'
            });
        $locationProvider.html5Mode( true   );
        $locationProvider.hashPrefix('!');
    });

In the login conntrolle above, I use a redirect on success $location.path('/user/{{user.userId}}'); as a result, I load the html page, but without the data clip2net.com/s/3t58sZZ f5ae0b36d95349e6b055630d8ea8585b.png
AND the transition page (data) $location.path('/user/{{user.userId}}');
image link " clip2net.com/s/3t57Pw4 "
60731b2e10f04315ba8e9239e11f7a56.png
Controller user page
'use strict';

angular.module('prApp')
    .controller('usersCtrl', ['$scope', '$rootScope','$routeParams', '$modal','loginService',
        function ($scope, $rootScope, $routeParams, $modal,loginService) {

           $scope.user = Users.get({id: $routeParams.userId});
            $scope.loginService = loginService;
  .........

It is quite possible that I am moving in the wrong direction, I would be glad if they set me on the right path ^^ Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-01-19
@leo_fr

Read here - stepansuvorov.com/blog/2014/09/angularjs-data-transfer
a lot of questions will disappear

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question