11

i'm building application which uses CORS requests. Each request i use get host address from a constant

angular.module('siteApp').constant('baseUrl', {
'server':'htttp://localhost/',
})

And in each service i use to send request like this:

angular.module('siteApp').factory('DocsSvc', function ($http, baseUrl) {
   var baseurl = baseUrl.server ;
   $http.get(baseurl + 'document')

Is it possible to make 'htttp://localhost/' value - to came from config.json file into baseUrl constant or baseUrl factory? I mean : how can i load something from ajax request an make it accessible to app modules

i have tried:

.run(['$rootScope', , function ($rootScope) {

  $.ajax('config.json', {async: false})
  .success(function (data) {
    $rootScope.HOST = data.HOST;
  });

And tried to access it from baseUrl:

angular.module('siteApp').factory('baseUrl',function($rootScope) {
 return {
  server: $rootScope.HOST

But no luck - the baseUrl.server comes undefined into functions

4 Answers 4

8

You can use run method of angular.

  var app = angular.module('plunker', []);

  app.run(function($http, $rootScope){
  $http.get('config.json')
  .success(function(data, status, headers, config) {
    $rootScope.config = data;
    $rootScope.$broadcast('config-loaded');
  })
  .error(function(data, status, headers, config) {
    // log error
    alert('error');
  });
})

app.controller('MainCtrl', function($scope, $rootScope) {
  $scope.$on('config-loaded', function(){
    $scope.name = $rootScope.config.name;  
  });
});

see this plunker

Sign up to request clarification or add additional context in comments.

3 Comments

please look into updated question: i had tried doing something like this, but without success
You need to listen config-loaded event only for controllers that are loaded initially. Because in this case controller is getting called before the json file is loaded.
thank you for trying so hard to help me. i have modified your plunker to be closer to my situation, and it not worked well :plnkr.co/edit/QfSzS0jbO8wRXwiM6T0W?p=preview . Anyway - thank you again, and it seems that i better render the host from server side
2

If you want to do it even before the angular app starts, you can, instead of using the ng-app directive, use the bootstrap function.

From: https://docs.angularjs.org/api/ng/function/angular.bootstrap

<!doctype html>
<html>
<body>
<div ng-controller="WelcomeController">
  {{greeting}}
</div>

<script src="angular.js"></script>
<script>
  var app = angular.module('demo', [])
  .controller('WelcomeController', function($scope) {
      $scope.greeting = 'Welcome!';
  });
  // Do your loading of JSON here
  angular.bootstrap(document, ['demo']);
</script>
</body>
</html>

Comments

1

You need to tell angular about data change, so modify your code something like this:

.run(['$rootScope', function ($rootScope) {

  $.ajax('config.json', {async: false})
  .success(function (data) {
    $rootScope.HOST = data.HOST;
    $rootScope.$apply();            // New line
  });
}])

That $apply() is needed since its a non-angular asynchronous call.

Comments

0

use the blow code snippet to load the json values

.run(function ($http, $rootScope) {
$http.get('launchSettings.json')
    .success(function (data, status, headers, config) {
        $rootScope.config = data;
        $rootScope.$broadcast('config-loaded');
    })
    .error(function (data, status, headers, config) {
        // log error
        alert('error');
    });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.