Javascript Angularjs指令属性属性所有未定义
我创建了一个具有以下定义的指令Javascript Angularjs指令属性属性所有未定义,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我创建了一个具有以下定义的指令 LastMeet.directive("progressBar", function() { return { restrict: "A", scope: { meeting: "&meeting" }, link: function(scope, elm, attrs) { var meeting = scope.meeting(); // Gather the details
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);
}
}
}
})
这是meeting对象的输出,它清楚地显示了包含的start\u datetime
属性。然而,第二个控制台输出是简单的
undefined
为什么会议对象在那里,我可以看到所有内容,但当我尝试访问包含的属性时,每次都没有定义?成功!因此,当指令运行时,变量未完全就绪似乎是一个问题。它正在查看的会议对象是通过
资源创建的,该资源在获取服务器数据时创建占位符对象,然后填充该对象
我的猜测是,这个对象存在(实际上是占位符),但我想要的值实际上还不存在。不确定为什么控制台输出显示它们在那里,但是很好。为了修复它,我在对象中添加了一个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)
}
}
})
lastmete.directive(“progressBar”),函数($timeout){
返回{
限制:“A”,
范围:{
会议:“=会议”
},
链接:功能(范围、elm、属性){
unwatch=范围$watch('会议'),功能(会议){
国际单项体育联合会(会议){
//收集我们需要的有关这次会议的详细信息
var startDate=meeting.start\u datetime;
var截止日期=会议。截止日期;
var complete=会议完成百分比;
//如果我们已经达到100%,做任何事情都没有意义
如果(完成<100.0){
//计算每秒要增加多少
var diff=截止日期-开始日期;
var增量=差异/60.0;
无功定时器;
scope.percentage=完成;
scope.onTimeout=函数(){
如果(范围百分比<100.0){
scope.percentage+=增量;
css({右:100-scope.percentage+“%”});
计时器=$timeout(scope.onTimeout,1000);
}
}
//设置计时器并开始:)
计时器=$timeout(scope.onTimeout,1000);
}
unwatch();
}
},对)
}
}
})
现在我有一些计算问题,但它起作用了:)会议是一个对象,而你在指令中将其视为一个函数?@MikeRobinson从未使用过angular,但我猜它与Ember相同。您可以将函数绑定到“对象”。是的,使用angular,您有3种绑定类型-文本、单向和双向。单向绑定(像这样)变成了方法,所以你不能编辑itI,但我无法在中重现你的问题(在Chrome中测试)。你能发布一个不工作的提琴或plunker吗?我可以试试,但我不确定错误是从哪里来的,是传入的数据,还是指令中的数据还是什么。真让人困惑:(我看看能不能把小提琴拼起来
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)
}
}
})