嵌套函数变量AngularJS?
好的,我有以下信息:嵌套函数变量AngularJS?,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,好的,我有以下信息: function getRate(source, $scope, e) { var dateValue = $("Date", source).text() || ""; if (!dateValue) { return null; } var dailyPrice = $("Dai
function getRate(source, $scope, e) {
var dateValue = $("Date", source).text() || "";
if (!dateValue) {
return null;
}
var dailyPrice = $("DailyPrice", source).text() || "";
var weeklyPrice = $("WeeklyPrice", source).text() || "";
var monthlyPrice = $("MonthlyPrice", source).text() || "";
var isAvailable = $("IsAvailable", source).text() === "1";
var minimumStay = Number($("MinimumStay", source).text());
console.log(dailyPrice, weeklyPrice, monthlyPrice, isAvailable, minimumStay, dateValue);
if (isNaN(minimumStay)) {
minimumStay = DEFAULT_MINIMUM_STAY;
}
return {
date: new Date(dateValue),
dailyPrice: dailyPrice,
weeklyPrice: weeklyPrice,
monthlyPrice: monthlyPrice,
reserved: !isAvailable,
minimumStay: minimumStay
};
}
在函数内部声明,如下所示:
function bookingmainfunction() {
//***************************************
// Constants
//***************************************
var MILLISECONDS_PER_DAY = 86400000;
var DEFAULT_MINIMUM_STAY = 1;
//***************************************
// Variables
//***************************************
var rangeMinDate = null;
var rangeMaxDate = null;
var checkInDate = null;
var checkOutDate = null;
var rates = {};
var cache = {
checkOutRanges: {},
checkInAvailability: {}
};
//***************************************
// Parsing
//***************************************
function parseXml(xml) {
rates = getRates(xml);
}
function getRates(xml) {
var result = {};
var rate;
$("AvailabilityRates DateData", xml).each(function() {
rate = getRate($(this));
if (!rate || !angular.isDate(rate.date)) {
return;
}
result[dateToHashKey(rate.date)] = rate;
updateDateRangeBoundaries(rate.date);
});
return result;
}
function getRate(source, $scope, e) {
var dateValue = $("Date", source).text() || "";
if (!dateValue) {
return null;
}
var dailyPrice = $("DailyPrice", source).text() || "";
var weeklyPrice = $("WeeklyPrice", source).text() || "";
var monthlyPrice = $("MonthlyPrice", source).text() || "";
var isAvailable = $("IsAvailable", source).text() === "1";
var minimumStay = Number($("MinimumStay", source).text());
console.log(dailyPrice, weeklyPrice, monthlyPrice, isAvailable, minimumStay, dateValue);
if (isNaN(minimumStay)) {
minimumStay = DEFAULT_MINIMUM_STAY;
}
return {
date: new Date(dateValue),
dailyPrice: dailyPrice,
weeklyPrice: weeklyPrice,
monthlyPrice: monthlyPrice,
reserved: !isAvailable,
minimumStay: minimumStay
};
}
}
angular.module("app", ["dateSheet"])
.service("Booking", [
/*ABOVE STATED FUNCTION GOES HERE*/
])
.controller("AppCtrl", [
"$scope",
"$attrs",
"AppDataLoader",
"Booking",
function(scope, attributes, AppDataLoader, Booking) {
var VIEW_STATE_LOADING = "loading";
var VIEW_STATE_ERROR = "error";
var VIEW_STATE_READY = "ready";
var VIEW_SUB_STATE_RATES = "rates";
var VIEW_SUB_STATE_MONTHS = "months";
alert("Booking");
scope.year = 2013;
scope.month = 7;
scope.months = months[date.getMonth()];
scope.monthdisplay = 3;
scope.currntday = date.getDate();
}]);
scope.monthdisplay = 3;
scope.currntday = date.getDate();
scope.returnbookingdate = Booking.getRate();
}]);
该轮到哪一个是在
.service("Booking", [/*ABOVE STATED FUNCTION GOES HERE*/])
然后将其连接到以下angularJS模块,如下所示:
function bookingmainfunction() {
//***************************************
// Constants
//***************************************
var MILLISECONDS_PER_DAY = 86400000;
var DEFAULT_MINIMUM_STAY = 1;
//***************************************
// Variables
//***************************************
var rangeMinDate = null;
var rangeMaxDate = null;
var checkInDate = null;
var checkOutDate = null;
var rates = {};
var cache = {
checkOutRanges: {},
checkInAvailability: {}
};
//***************************************
// Parsing
//***************************************
function parseXml(xml) {
rates = getRates(xml);
}
function getRates(xml) {
var result = {};
var rate;
$("AvailabilityRates DateData", xml).each(function() {
rate = getRate($(this));
if (!rate || !angular.isDate(rate.date)) {
return;
}
result[dateToHashKey(rate.date)] = rate;
updateDateRangeBoundaries(rate.date);
});
return result;
}
function getRate(source, $scope, e) {
var dateValue = $("Date", source).text() || "";
if (!dateValue) {
return null;
}
var dailyPrice = $("DailyPrice", source).text() || "";
var weeklyPrice = $("WeeklyPrice", source).text() || "";
var monthlyPrice = $("MonthlyPrice", source).text() || "";
var isAvailable = $("IsAvailable", source).text() === "1";
var minimumStay = Number($("MinimumStay", source).text());
console.log(dailyPrice, weeklyPrice, monthlyPrice, isAvailable, minimumStay, dateValue);
if (isNaN(minimumStay)) {
minimumStay = DEFAULT_MINIMUM_STAY;
}
return {
date: new Date(dateValue),
dailyPrice: dailyPrice,
weeklyPrice: weeklyPrice,
monthlyPrice: monthlyPrice,
reserved: !isAvailable,
minimumStay: minimumStay
};
}
}
angular.module("app", ["dateSheet"])
.service("Booking", [
/*ABOVE STATED FUNCTION GOES HERE*/
])
.controller("AppCtrl", [
"$scope",
"$attrs",
"AppDataLoader",
"Booking",
function(scope, attributes, AppDataLoader, Booking) {
var VIEW_STATE_LOADING = "loading";
var VIEW_STATE_ERROR = "error";
var VIEW_STATE_READY = "ready";
var VIEW_SUB_STATE_RATES = "rates";
var VIEW_SUB_STATE_MONTHS = "months";
alert("Booking");
scope.year = 2013;
scope.month = 7;
scope.months = months[date.getMonth()];
scope.monthdisplay = 3;
scope.currntday = date.getDate();
}]);
scope.monthdisplay = 3;
scope.currntday = date.getDate();
scope.returnbookingdate = Booking.getRate();
}]);
现在,当我尝试添加scope.returnbookingdate=Booking.getRate()时;
像这样进入我的模块:
function bookingmainfunction() {
//***************************************
// Constants
//***************************************
var MILLISECONDS_PER_DAY = 86400000;
var DEFAULT_MINIMUM_STAY = 1;
//***************************************
// Variables
//***************************************
var rangeMinDate = null;
var rangeMaxDate = null;
var checkInDate = null;
var checkOutDate = null;
var rates = {};
var cache = {
checkOutRanges: {},
checkInAvailability: {}
};
//***************************************
// Parsing
//***************************************
function parseXml(xml) {
rates = getRates(xml);
}
function getRates(xml) {
var result = {};
var rate;
$("AvailabilityRates DateData", xml).each(function() {
rate = getRate($(this));
if (!rate || !angular.isDate(rate.date)) {
return;
}
result[dateToHashKey(rate.date)] = rate;
updateDateRangeBoundaries(rate.date);
});
return result;
}
function getRate(source, $scope, e) {
var dateValue = $("Date", source).text() || "";
if (!dateValue) {
return null;
}
var dailyPrice = $("DailyPrice", source).text() || "";
var weeklyPrice = $("WeeklyPrice", source).text() || "";
var monthlyPrice = $("MonthlyPrice", source).text() || "";
var isAvailable = $("IsAvailable", source).text() === "1";
var minimumStay = Number($("MinimumStay", source).text());
console.log(dailyPrice, weeklyPrice, monthlyPrice, isAvailable, minimumStay, dateValue);
if (isNaN(minimumStay)) {
minimumStay = DEFAULT_MINIMUM_STAY;
}
return {
date: new Date(dateValue),
dailyPrice: dailyPrice,
weeklyPrice: weeklyPrice,
monthlyPrice: monthlyPrice,
reserved: !isAvailable,
minimumStay: minimumStay
};
}
}
angular.module("app", ["dateSheet"])
.service("Booking", [
/*ABOVE STATED FUNCTION GOES HERE*/
])
.controller("AppCtrl", [
"$scope",
"$attrs",
"AppDataLoader",
"Booking",
function(scope, attributes, AppDataLoader, Booking) {
var VIEW_STATE_LOADING = "loading";
var VIEW_STATE_ERROR = "error";
var VIEW_STATE_READY = "ready";
var VIEW_SUB_STATE_RATES = "rates";
var VIEW_SUB_STATE_MONTHS = "months";
alert("Booking");
scope.year = 2013;
scope.month = 7;
scope.months = months[date.getMonth()];
scope.monthdisplay = 3;
scope.currntday = date.getDate();
}]);
scope.monthdisplay = 3;
scope.currntday = date.getDate();
scope.returnbookingdate = Booking.getRate();
}]);
它返回TypeError:对象#没有方法“getRate”
为什么?
如何使这些嵌套函数和变量从服务中返回到模块中。。
基本上,我试图将相关的XML数据附加到dom中。但是它赢了
问候
Chris您从服务返回的对象未公开
getRate
。在第二次检查时,实际上您似乎并没有从服务中退回任何东西
angular.service
应该返回一个构造函数,该构造函数将用于实例化单例
我建议您使用工厂
并执行以下操作:
.factory('Booking', function() {
var Booking = function() {
//constructor
};
Booking.getRate = function() {
// ...
};
//or if you intended it to be an instance method:
Booking.prototype.getRate = function() {
// ...
};
//add other public methods here
//you can add private methods too
// ...
return Booking;
});