【发布时间】:2017-01-03 14:55:03
【问题描述】:
我有下一个函数,我向服务器发出异步请求,然后将返回值应用于复杂的控制器对象:
this.createIdleTimeWidget = function () {
var $this = this;
$dashboardFactory.getIdleTimeCurrentStatus().then(function (response) {
var widgetId = $commonUtils.generateQuickGuid();
Object.keys(response.data).forEach(function (item) {
if (!hasOwnProperty.call($this.widgets, widgetId)) {
$this.widgets[widgetId] = {};
}
var obj = $dashboardFactory.genereteIdleStatusObject(item, response.data[item]);
Object.defineProperty($this.widgets[widgetId], Object.keys(obj)[0], {
value: obj[Object.keys(obj)[0]],
writable: true,
enumerable: true,
configurable: true
});
});
$this.dashboardOptions.addWidget({
name: 'idleTime',
id: widgetId
});
});
}
添加小部件使用 next 调用指令:
return {
restrict: 'EA',
scope: {
type: '@',
timewidgetfor: '@',
widgetid:'@'
},
controller: 'dashboardController',
link: function ($scope, element, iAttrs, controller) {
var date = new Date();
var seconds = date.getSeconds();
var minutes = date.getMinutes();
var hours = date.getHours();
function compileTemplate() {
var template = $templateCache.get('clockWidgetTemplate.html');
var interpolatedTemplate = $interpolate(template)($scope);
element.html($interpolate(template)($scope));
var elem = angular.element(document.querySelector('#' + iAttrs.id + ' .inner-box'));
return elem;
};
....
模板看起来像:
$templateCache.put('clockTemplate.html',
'<div class="clock-container"><div ngx-timer widgetId={{widget.id}} timeWidgetFor="now" type="Hours" class="box hours" id="hours"></div> <div widgetId={{widget.id}} timeWidgetFor="now" ngx-timer type="Minutes" class="box minutes" id="minutes"></div><div ngx-timer widgetId={{widget.id}} timeWidgetFor="now" type="Seconds" class="box seconds" id="seconds"></div></div></div>'
+ '<div class="clock-container"><div ngx-timer widgetId={{widget.id}} timeWidgetFor="today" type="Hours" class="box hours" id="hours"></div> <div widgetId={{widget.id}} timeWidgetFor="today" ngx-timer type="Minutes" class="box minutes" id="minutes"></div><div ngx-timer widgetId={{widget.id}} timeWidgetFor="today" type="Seconds" class="box seconds" id="seconds"></div></div></div>'
+ '<div class="clock-container"><div ngx-timer widgetId={{widget.id}} timeWidgetFor="week" type="Hours" class="box hours" id="hours"></div> <div widgetId={{widget.id}} timeWidgetFor="week" ngx-timer type="Minutes" class="box minutes" id="minutes"></div><div ngx-timer widgetId={{widget.id}} timeWidgetFor="week" type="Seconds" class="box seconds" id="seconds"></div></div></div>'
+ '<div class="clock-container"><div ngx-timer widgetId={{widget.id}} timeWidgetFor="month" type="Hours" class="box hours" id="hours"></div> <div widgetId={{widget.id}} timeWidgetFor="month" ngx-timer type="Minutes" class="box minutes" id="minutes"></div><div ngx-timer widgetId={{widget.id}} timeWidgetFor="month" type="Seconds" class="box seconds" id="seconds"></div></div></div>');//<div ngx-timer type="AMPM" class="box ampm" id="ampm">
$templateCache.put('clockWidgetTemplate.html', '<div class="inner-box"> <span class="top">{{current}}</span><span class="top-next">{{next}}</span><span class="bottom-next">{{next}}</span> <span class="bottom">{{current}}</span> </div>');
但是,controller.widgets in 指令是空对象,而在控制器本身中它被定义得很好。
我在控制器中使用 controllerAs 语法。我也试过 $apply() 方法,还是没有结果。
工作小伙伴:Plunkr
我错过了什么?
【问题讨论】:
-
你是如何使用指令的?可以加模板吗?
-
@AvneshShakya 问题已更新
-
首先,您是否尝试过添加 console.log() 并查看解决了什么问题,什么没有解决?其次,不要按原样调用 $dashboardFactory ,而是将其全部放在一个函数中,调用它并 console.log 将其输出,看看它是否达到了您的预期。三、初始化$this.widgets = [];在你的 var $this = this;行,看看有没有帮助。
-
问题可能归因于代码长度超过 3.1k 行的事实。至少,正如您现在发现的那样,那么长的单个文档更难调试。如果您的文件超过 300 行,则说明您错误地使用了 Angular....
标签: angularjs angular-directive