简体   繁体   English

如果我在链接函数之外指定范围,则$ w​​atch无法正常工作

[英]$watch doesn't work if I specify a scope outside the link function

I just started working on an Angular app that uses flot to plot a bunch of data. 我刚开始使用Angular应用程序工作,该应用程序使用flot绘制了大量数据。 It worked fine for static data, but once we got the directive wired up to mongo, I had to follow the tutorial here to get it working for updating data. 它适用于静态数据,但是一旦将指令连接到mongo,我就必须按照此处的教程进行操作以使其能够更新数据。 I had a hell of a time for one specific reason: 我因某个特定原因而度过了一段时光:

This is my directive HTML: 这是我的指令HTML:

<div class="panel-body" data-ng-controller="flotChartCtrl">
  <div data-flot-line-chart data-data="revenueData.data" data-options="line1.options" style="width: 100%; height: 300px;"></div>
</div>

and javascript: 和javascript:

.directive("flotLineChart", [
    function () {
        return{
            restrict: 'A',
            scope: {
                data: "=",
                options: "="
            },
            link: function(scope, elem, attrs){
                var chart = null;
                // var options = { ... };

                scope.$watch('data', function(data, oldData) {
                    if(!chart) {
                        chart = $.plot(elem, data, options);
                        elem.show();
                    } else {
                        chart.setData(data);
                        chart.setupGrid();
                        chart.draw();
                    }
                });
            }
        };
    }
])

As you can see in the html, I'm using the data-options attribute to pass the line1.options object into the directive. 正如您在html中看到的那样,我正在使用data-options属性将line1.options对象传递到指令中。 When I was just using static data and not using ng-model or the $watch function, this worked and the scope: { options: "=" } assignments were correct. 当我只使用静态数据而不使用ng-model$watch函数时,此方法有效且scope: { options: "=" }分配是正确的。 However it seems that whenever I set anything on the scope outside link , it breaks the $watch . 但是,似乎每当我在link外部的作用域上设置任何内容时,它都会破坏$watch $watch always receives a data of undefined... and my scope.options are also undefined. $watch总是收到未定义的data ...,而我的scope.options也未定义。 Outside of the $watch function scope.options is correct, but that doesn't help me much if I can't use them when the data is actually plotted. $watch函数的scope.options是正确的,但是如果在实际绘制数据时无法使用它们,那对我没有太大帮助。

I've had to resort to hard coding the options inside link: and commenting out the outer scope assignments. 我不得不求助于对link:内部的选项进行硬编码link:并注释掉外部作用域分配。 I have a bunch of different charts I need to create, all of which look differently. 我有一堆需要创建的不同图表,所有这些图表看起来都不同。 I'd hate to have to hard code different options for EVERY one, but at the moment I don't see any other way to make this work. 我不愿意为每个选项都硬编码不同的选项,但是目前我看不到任何其他方法可以使这项工作实现。 Is there some way I can access my other data attributes from the HTML inside the $watch function without it breaking everything? 有什么方法可以通过$watch函数中的HTML访问我的其他数据属性,而不破坏所有内容?

Note: I tried attrs.options , but that just gives me a "line1.options" string, and not the actual object. 注意:我尝试了attrs.options ,但这只是给我一个“ line1.options”字符串,而不是实际的对象。

Edit1: 编辑1:

Updated my code per ExpertSystem's suggestions. 根据ExpertSystem的建议更新了我的代码。 No longer using ng-model . 不再使用ng-model scope is still not available inside $watch: $ watch scope仍然不可用:

没有在$ watch中定义范围的证据

Your directive should look like this: 您的指令应如下所示:

...
restrict: 'A',
scope: {
    data:    '=ngModel',
    options: '='
},
link: function(scope, elem, attrs){
    ...
    scope.$watch('data', function(newValue, oldValue) {
        ...

Althouth the use of ngModel seems redundant here. ngModel在这里ngModel的使用似乎是多余的。


This fiddle demonstrates that scope is indeed defined in the $watch callback. 这个小提琴表明, scope确实在$watch回调中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM