您好我正在尝试在angularjs和phonegap中创建日志服务,以便在我的手机中写入一些日志。
到目前为止,我已尝试实现一个提供程序,以便我可以轻松配置要写入的文件(window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
)以及是否要在外部文件中写入日志。
问题是,如果我在OnDeviceReady事件中包装配置行,则提供程序的配置无法正常工作,
所以我的问题是,因为我想要一个全局变量来写同一个文件中的所有内容如何配置需要在OnDeviceReady之后执行的提供程序?
app.js的实施
MyApp.config(['$routeProvider','$httpProvider','logItProvider', function ($routeProvider, $httpProvider, logIt) {
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
alert("deviceReady!");
var file = "myFile";
logIt.setFile(file);
logIt.setLogEnable(true);
}
这是我的提供者的实施:
angular.module('logger', [])
.provider('logIt', function(){
// internal configuration data; configured through setter function
this.file = 'Default';
this.callsParseCounter = 0;
this.isLogEnable = false;
this.$get = function() {
var file = this.file;
var numberOfCalls = this.callsParseCounter;
var isLogEnable = this.isLogEnable;
return {
getFile: function() {
return "Hello, " + file + "!"
},
writeLog: function(message){
if(this.isLogEnable)
{
}
},
incrementParseCounter: function()
{
if(isLogEnable)
{
numberOfCalls++;
this.callsParseCounter = numberOfCalls;
}
},
getNumberOfCalls: function(){
return numberOfCalls;
}
}
};
this.setFile = function(file) {
this.file = file;
};
this.setCallParseCounter = function(callsParseCounter)
{
this.callsParseCounter = callsParseCounter;
};
this.setLogEnable = function(isLogEnable)
{
this.isLogEnable = isLogEnable;
}
});