是否可以在deviceready中调用工厂绑定变量?

时间:2016-07-29 03:08:12

标签: angularjs

我的javascript中有以下代码:

var app = angular.module("App");
app.factory('rest', function($http) {
});

index.js:

document.addEventListener("deviceready", function() {
    // How I am able to call rest here?
    var domElement = document.documentElement;
    angular.bootstrap(domElement, ["App"]);

    var $body = angular.element(document.body);
    var $rootScope = $body.scope().$root;
    $rootScope.$apply(function () {
        $rootScope.$broadcast('initialized', 'initialized');
    });
});

1 个答案:

答案 0 :(得分:1)

您可以在模块中注册run功能。因此,当您将引导模块时,将执行运行,并且在run中您将能够访问工厂。像这样:

// Code goes here
var app = angular.module("App");
app.factory('rest', function($http) {});

app.run(function(rest) {
  console.log('your rest factory', rest);
});

document.addEventListener("deviceready", function() {
  // How I am able to call rest here?
  var domElement = document.documentElement;
  angular.bootstrap(domElement, ["App"]);

  var $body = angular.element(document.body);
  var $rootScope = $body.scope().$root;
  $rootScope.$apply(function() {
    $rootScope.$broadcast('initialized', 'initialized');
  });
});