1

我使用几个问题的解决方案使用 highchart.js 渲染了一个图表。我了解指令的基本用法。但是,在 highchart.js 的情况下,我不太明白这里的代码:

app.directive('highchart', function () {
    var direc = {};
    var link = function (scope, element, attributes) {
        scope.$watch(function () {
            return attributes.chart;
        }, function () {
            var charts = JSON.parse(attributes.chart);
            $(element[0]).highcharts(charts);
        })
    }
    direc.restrict = 'E';
    direc.link = link;
    direc.template = '<div></div>';
    //the replace method replaces the content inside the element it is called
    direc.replace = true;
    direc.scope = {};
    return direc;
})

图表属性将接受图表属性的 JSON 数组。

有人可以解释函数内部发生了什么吗?感谢您的阅读。

4

2 回答 2

1

$watch 用于监视特定字段的更改。在上述情况下,attributes.chart 正在监视 $watch 函数中第一个参数的变化,第二个参数用于实际检查修改后的数据并对其执行操作。您还可以在官方 angular 文档中找到 $watch 可以使用的更多选项:https : //docs.angularjs.org/api/ng/type/ $rootScope.Scope#$watch

于 2020-05-18T06:19:49.680 回答
0

$watch 监视模型变化,一旦模型变化,更新的值从下面的代码中获取,并且根据要求,可以执行所需的操作。

$scope.$watch('ng-model-name', function (newval, oldval) { if (newval< 0) { $('#modelCustomer').modal('show'); } });

于 2020-05-18T09:51:20.743 回答