2016-07-29 108 views
2

我已经在我的JavaScript的地方下面的代码:是否可以在deviceready中调用工厂绑定变量?

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

您可以注册一个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'); 
    }); 
}); 
+0

看起来不能工作:( – Rendy

+0

它应该工作。 –

相关问题