【问题标题】:Controller scope in directive is not updated指令中的控制器范围未更新
【发布时间】: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


【解决方案1】:

您通过在directive 中使用@ 来使用一种方式绑定。

所以尝试使用{{}}@ 绑定属性,如下所示:

<div class="clock-container">
   <div ngx-timer widgetId={{widget.id}} timeWidgetFor="{{now}}" type="{{hours}}" class="box hours" id="hours"></div>
</div>

在此jsfiddle link中查看单向绑定(@)、双向绑定(=)和事件绑定(&amp;)的不同用法

【讨论】:

  • 感谢您的帮助,但我的问题不是孤立范围,而是控制器范围。更简单的理解见我附上PS
  • @ 不是单向绑定。这是一个文本绑定。
  • @FrederikPrijck:是的文字,但请检查:stackoverflow.com/questions/33126555/…希望这会清楚。
  • 它仍然不是单向绑定。阅读docs.angularjs.org/guide/component 并查找以下部分:“prntscr.com/drct79,这是来自文档的屏幕截图。
  • 检查这个 jsfiddle 链接:jsfiddle.net/avnesh2/fzyo5p0t/… 并玩转,看看为什么它是单向绑定
猜你喜欢
  • 1970-01-01
  • 2015-04-30
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多