AngularJS : Initialize service with asynchronous data

后端 未结 10 1760
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 03:09

I have an AngularJS service that I want to initialize with some asynchronous data. Something like this:

myModule.service(\'MyService\', function($http) {
            


        
10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 03:24

    Easiest way to fetch any initialize use ng-init directory.

    Just put ng-init div scope where you want to fetch init data

    index.html

    index.js

    $scope.init=function(){
        $http({method:'GET',url:'/countries/countries.json'}).success(function(data){
          alert();
               $scope.countries = data;
        });
      };
    

    NOTE: you can use this methodology if you do not have same code more then one place.

提交回复
热议问题