如何在 ionic 中调用相同服务的其他功能 (angular.js)
How to call other functions of same services in ionic (angular.js)
我正在使用离子框架开发移动应用程序。我创建了一个像这样的通用实用程序服务
.service('CommonUtilityService', function($q) {
return {
parseJsonDate:function(jsonDate){
var offset = new Date().getTimezoneOffset() * 60000;
var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
if (parts[2] == undefined)
parts[2] = 0;
if (parts[3] == undefined)
parts[3] = 0;
return new Date(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000);
},
daysBetween:function(date1String, date2String){
var ONE_DAY = 1000 * 60 * 60 * 24;
var ONE_MINUTE = 1000 * 60;
var d1 = new Date(date1String);
var d2 = new Date(date2String);
var d1_ms = d1.getTime() - d1.getTimezoneOffset() * ONE_MINUTE;
var d2_ms = d2.getTime() - d2.getTimezoneOffset() * ONE_MINUTE;
return Math.floor(d1_ms - d2_ms/ONE_DAY);
},
getNumberOfDays:function(jsonDate){
// var date = parseJsonDate(jsonDate);
var date = new Date();
var today = new Date();
return this.daysBetween(today,date);
}
}
})
当我尝试在 getNumberOfDays 函数中调用 daysBetween 函数时出现错误
this.daysBetween is not a function
谁能告诉我如何在 getNumberOfDays 函数中调用 daysBetween 函数。
实际上Service doesn't return a object where Factory return an object。您可以像这样尝试重构服务
Service
A service is a constructor function which creates the object using the
new keyword. You can add properties and functions to a service object
by using the this keyword. Unlike a factory, it doesn't return
anything (it returns an object which contains method).
代码
.service('CommonUtilityService', function($q) {
var CommonUtilityService = this;
CommonUtilityService.parseJsonDate = function(jsonDate) {
var offset = new Date().getTimezoneOffset() * 60000;
var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
if (parts[2] == undefined)
parts[2] = 0;
if (parts[3] == undefined)
parts[3] = 0;
return new Date(+parts[1] + offset + parts[2] * 3600000 + parts[3] * 60000);
}
CommonUtilityService.daysBetween = function(date1String, date2String) {
var ONE_DAY = 1000 * 60 * 60 * 24;
var ONE_MINUTE = 1000 * 60;
var d1 = new Date(date1String);
var d2 = new Date(date2String);
var d1_ms = d1.getTime() - d1.getTimezoneOffset() * ONE_MINUTE;
var d2_ms = d2.getTime() - d2.getTimezoneOffset() * ONE_MINUTE;
return Math.floor(d1_ms - d2_ms / ONE_DAY);
}
CommonUtilityService.getNumberOfDays = function(jsonDate) {
// var date = parseJsonDate(jsonDate);
var date = new Date();
var today = new Date();
return CommonUtilityService.daysBetween(today, date);
}
})
您正在使用 模式三:Hybrid/Facade 所以在此您必须使用 this 正如@pankajparkar
或
.service('CommonUtilityService', function($q) {
var parseJsonDate = function(jsonDate){
var offset = new Date().getTimezoneOffset() * 60000;
var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
if (parts[2] == undefined)
parts[2] = 0;
if (parts[3] == undefined)
parts[3] = 0;
return new Date(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000);
},
var getNumberOfDays = function(jsonDate){
// var date = parseJsonDate(jsonDate);
var date = new Date();
var today = new Date();
return this.daysBetween(today,date);
}
return {
parseJsonDate: parseJsonDate,
getNumberOfDays: getNumberOfDays
}
})
我正在使用离子框架开发移动应用程序。我创建了一个像这样的通用实用程序服务
.service('CommonUtilityService', function($q) {
return {
parseJsonDate:function(jsonDate){
var offset = new Date().getTimezoneOffset() * 60000;
var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
if (parts[2] == undefined)
parts[2] = 0;
if (parts[3] == undefined)
parts[3] = 0;
return new Date(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000);
},
daysBetween:function(date1String, date2String){
var ONE_DAY = 1000 * 60 * 60 * 24;
var ONE_MINUTE = 1000 * 60;
var d1 = new Date(date1String);
var d2 = new Date(date2String);
var d1_ms = d1.getTime() - d1.getTimezoneOffset() * ONE_MINUTE;
var d2_ms = d2.getTime() - d2.getTimezoneOffset() * ONE_MINUTE;
return Math.floor(d1_ms - d2_ms/ONE_DAY);
},
getNumberOfDays:function(jsonDate){
// var date = parseJsonDate(jsonDate);
var date = new Date();
var today = new Date();
return this.daysBetween(today,date);
}
}
})
当我尝试在 getNumberOfDays 函数中调用 daysBetween 函数时出现错误
this.daysBetween is not a function
谁能告诉我如何在 getNumberOfDays 函数中调用 daysBetween 函数。
实际上Service doesn't return a object where Factory return an object。您可以像这样尝试重构服务
Service
A service is a constructor function which creates the object using the new keyword. You can add properties and functions to a service object by using the this keyword. Unlike a factory, it doesn't return anything (it returns an object which contains method).
代码
.service('CommonUtilityService', function($q) {
var CommonUtilityService = this;
CommonUtilityService.parseJsonDate = function(jsonDate) {
var offset = new Date().getTimezoneOffset() * 60000;
var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
if (parts[2] == undefined)
parts[2] = 0;
if (parts[3] == undefined)
parts[3] = 0;
return new Date(+parts[1] + offset + parts[2] * 3600000 + parts[3] * 60000);
}
CommonUtilityService.daysBetween = function(date1String, date2String) {
var ONE_DAY = 1000 * 60 * 60 * 24;
var ONE_MINUTE = 1000 * 60;
var d1 = new Date(date1String);
var d2 = new Date(date2String);
var d1_ms = d1.getTime() - d1.getTimezoneOffset() * ONE_MINUTE;
var d2_ms = d2.getTime() - d2.getTimezoneOffset() * ONE_MINUTE;
return Math.floor(d1_ms - d2_ms / ONE_DAY);
}
CommonUtilityService.getNumberOfDays = function(jsonDate) {
// var date = parseJsonDate(jsonDate);
var date = new Date();
var today = new Date();
return CommonUtilityService.daysBetween(today, date);
}
})
您正在使用 模式三:Hybrid/Facade 所以在此您必须使用 this 正如@pankajparkar
或
.service('CommonUtilityService', function($q) {
var parseJsonDate = function(jsonDate){
var offset = new Date().getTimezoneOffset() * 60000;
var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
if (parts[2] == undefined)
parts[2] = 0;
if (parts[3] == undefined)
parts[3] = 0;
return new Date(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000);
},
var getNumberOfDays = function(jsonDate){
// var date = parseJsonDate(jsonDate);
var date = new Date();
var today = new Date();
return this.daysBetween(today,date);
}
return {
parseJsonDate: parseJsonDate,
getNumberOfDays: getNumberOfDays
}
})