gpt4 book ai didi

javascript - Angularjs 指令属性属性全部未定义

转载 作者:行者123 更新时间:2023-11-30 18:01:48 26 4
gpt4 key购买 nike

我创建了一个具有以下定义的指令

LastMeet.directive("progressBar", function() {
return {
restrict: "A",
scope: {
meeting: "&meeting"
},
link: function(scope, elm, attrs) {

var meeting = scope.meeting();

// Gather the details we need about this meeting
var startDate = scope.meeting().start_datetime;
var deadline = scope.meeting().deadline;
var complete = scope.meeting().percentage_complete;
console.log(meeting);
console.log(meeting["start_datetime"]);

// No point doing anything if we're already at 100%
if (complete < 100.0) {
// Calculate how much to increment by every second
var diff = deadline - startDate;
var increment = diff / 60.0;

var timer;

scope.percentage = complete;

scope.onTimeout = function() {

if (scope.percentage < 100.0) {
scope.percentage += increment;
elm.css({ right: 100 - percentage + "%" });
timer = $timeout(scope.onTimeout, 1000);
}

}

// Setup our timer and get going :)
timer = $timeout(scope.onTimeout, 1000);
}

}
}
})

session 属性是一个具有许多不同属性的对象。如您所见,我添加了 2 个控制台输出,一个用于 session 本身,另一个用于我尝试访问的属性之一。我的控制台有以下输出

b {$resolved: false, $then: function, $get: function, $save: function, $query: function…}
$resolved: true
$then: function (callback, errback) {
agenda_items: Array[2]
deadline: 1365897600
description: "Meeting to discuss the progress of LastMeet"
faye_token: "7468585e529849ca992efbd3b9de6337"
icon: null
id: 20
name: "LastMeet"
percentage_complete: 100
start_datetime: 1365897600
__proto__: b

这是 session 对象的输出,它清楚地显示了包含的 start_datetime 属性。然而,第二个控制台输出很简单

undefined

为什么 session 对象在那里,而且我可以看到所有内容,但当我尝试访问包含的属性时,我每次都只是未定义?

最佳答案

成功了!因此,指令运行时变量未完全准备好似乎是一个问题。它正在查看的 session 对象是通过 resource 创建的,它在获取服务器数据时创建占位符对象,然后填充该对象。

我的猜测是,angular 看到对象存在(实际上是占位符)但我想要的值实际上还不存在。不知道为什么控制台输出显示它们在那里,但是哦,好吧。为了修复它,我向对象添加了一个 watch 语句,该对象在实际更改并被填充时被删除。我的指令现在看起来像这样

LastMeet.directive("progressBar", function($timeout) {
return {
restrict: "A",
scope: {
meeting: "=meeting"
},
link: function(scope, elm, attrs) {

unwatch = scope.$watch('meeting', function(meeting) {
if (meeting) {
// Gather the details we need about this meeting
var startDate = meeting.start_datetime;
var deadline = meeting.deadline;
var complete = meeting.percentage_complete;

// No point doing anything if we're already at 100%
if (complete < 100.0) {
// Calculate how much to increment by every second
var diff = deadline - startDate;
var increment = diff / 60.0;

var timer;

scope.percentage = complete;

scope.onTimeout = function() {

if (scope.percentage < 100.0) {
scope.percentage += increment;
elm.css({ right: 100 - scope.percentage + "%" });
timer = $timeout(scope.onTimeout, 1000);
}

}

// Setup our timer and get going :)
timer = $timeout(scope.onTimeout, 1000);
}

unwatch();
}
}, true)
}
}
})

现在我有一些计算问题,但它正在工作:)

关于javascript - Angularjs 指令属性属性全部未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16816537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com