0

Here is an example of what I was trying to implement

I have a factory like this:

export default app.factory('mainFactory', $http => {
      return {
        getConfig: (cr) => $http({ method: 'GET', url: '/main-conf' }).then(res => cr = res.data, error => console.log(error))
      }
});

Next, the controller in which I want to use this data

import app from '../../main-module/index.js';

export default app.controller('mainCtrl', [
  '$scope',
  '$http',
  'mainFactory',
  '$window',
  ($scope, $http, mainFactory, $window) => {

    $scope.reloadRoute = () => $window.location.reload();
    $scope.mainFactory = mainFactory;
    
    console.log('$scope.mainFactory', $scope.mainFactory.getConfig($scope.mainConf2));
  }
]);

Finally I get this

введите сюда описание изображения

and if you implement everything in the controller, then everything is ok

   import app from '../../main-module/index.js'; 
    export default app.controller('mainCtrl', [
      '$scope',
      '$http',
      'mainFactory',
      '$window',
      ($scope, $http, mainFactory, $window) => {
        $scope.reloadRoute = () => $window.location.reload();
        $scope.mainConf;
        $http({ method: 'GET', url: '/main-conf' }).then(res => $scope.mainConf = res.data, error => console.log(error));
    
        console.log('$scope.mainConf', $scope.mainConf);
      }
   ]);

What am I doing wrong? How can I get the result of the second example in the first example? Thanks in advance

AvgustinTomsic
  • 1,809
  • 16
  • 22
Air
  • 181
  • 3
  • 15

0 Answers0