角度:注意指令中的过滤值 [英] angular: watch for filtered value in directive

查看:22
本文介绍了角度:注意指令中的过滤值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个基于给定值绘制图表的指令.我希望从模板中传递绘图的数据.我有一个可行的解决方案,在 ng-model 中传递数据,然后我可以添加一个 $scope.watch 语句.但这不适用于过滤后的数据,而且我不需要双向绑定.

I'm trying to implement a directive that draws a chart based on given values. I want the pass the data for the plot from the template. I have a working solution, passing the data in ng-model, for which I can then add a $scope.watch statement. But that doesn't work with filtered data, and I don't need two-way binding.

理想情况下,html 应如下所示:

Ideally, the html should look like:

 <chart ???????="list | filter" />

我想指令应该是这样的:

The directive, I guess, should look like:

  return{
    restrict: 'C',
    link: function(scope, elem, attrs){
        var chart = null

        scope.$watch(????, function(v){
             if(!chart){
                chart = $.plot(elem, v , options);
                elem.show();
            }else{
                chart.setData(v);
                chart.setupGrid(); 
                chart.draw();
            }
        });
    }
};

有没有角度的方法来实现这一点?

Is there an angular way to achieve this?

推荐答案

如何将过滤后的列表保存在控制器的不同变量中?类似:

What about saving the filtered list in a different variable in your controller? something like:

$scope.filteredList = $filter('yourFilter')($scope.list);

在 HTML 中:

<chart ????="filteredList" />

您只需确保在 list 更改时更新 filteredList.

You only need to make sure you update filteredList whenever list changes.

这篇关于角度:注意指令中的过滤值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆