$watch'ing for data changes in an Angular directive

前端 未结 3 1994
春和景丽
春和景丽 2020-11-29 15:40

How can I trigger a $watch variable in an Angular directive when manipulating the data inside (e.g., inserting or removing data), but not assign a new object to

3条回答
  •  死守一世寂寞
    2020-11-29 16:17

    My version for a directive that uses jqplot to plot the data once it becomes available:

        app.directive('lineChart', function() {
            $.jqplot.config.enablePlugins = true;
    
            return function(scope, element, attrs) {
                scope.$watch(attrs.lineChart, function(newValue, oldValue) {
                    if (newValue) {
                        // alert(scope.$eval(attrs.lineChart));
                        var plot = $.jqplot(element[0].id, scope.$eval(attrs.lineChart), scope.$eval(attrs.options));
                    }
                });
            }
    });
    

提交回复
热议问题