1

I am trying to develop an app where one module will be dependent on second module. In the first module run function Im trying to fill templatecache with some data from server using http but since http calls are async my second module will run before the first is completed and I get undefined in the result. Below code would make situation more clear

   var app = angular.module('main', []).run(["$templateCache",'$http','$timeout', function ($templateCache,$http,$timeout) {
              $timeout(function () {
                  $templateCache.put('ajay', 'ajay');
              }, 1000);
          }
          ]);

          var secondapp = angular.module('plunker', ['main']);
          secondapp.controller('test', function ($scope, $templateCache, $timeout) {
              // get me undefined i want to wait until module main is finished running run function 
              alert($templateCache.get('ajay'));
          });
4

1 回答 1

1

拥抱异步:

 var app = angular.module('main', []).run(["$templateCache",'$http','$timeout', function ($templateCache,$http,$timeout) {
     $templateCache.put('ajay', $http.get("/data/ajay"));
 }
 ]);

 var secondapp = angular.module('plunker', ['main']);
 secondapp.controller('test', function ($scope, $templateCache, $timeout) {
     $templateCache.get('ajay').then(function (data) {
         alert(data);
     });
 });
于 2013-07-03T20:26:20.537 回答