Angularjs $watch在指令中的使用
我已经使用highchart.js通过几个问题的解决方案呈现了一张图表。我了解指令的基本用法。但是,对于highchart.js,我不太理解这里的代码:Angularjs $watch在指令中的使用,angularjs,highcharts,Angularjs,Highcharts,我已经使用highchart.js通过几个问题的解决方案呈现了一张图表。我了解指令的基本用法。但是,对于highchart.js,我不太理解这里的代码: app.directive('highchart', function () { var direc = {}; var link = function (scope, element, attributes) { scope.$watch(function () { return att
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;
})
app.directive('highchart',function(){
var direc={};
var link=函数(范围、元素、属性){
范围:$watch(函数(){
返回属性.chart;
},函数(){
var charts=JSON.parse(attributes.chart);
$(元素[0])。高图(图表);
})
}
direc.restrict='E';
direc.link=链接;
direc.template='';
//replace方法替换它所调用的元素中的内容
direc.replace=true;
direc.scope={};
返回指令;
})
charts属性将接受图表属性的JSON数组
有人能解释一下函数内部发生了什么吗?感谢阅读。美元手表用于监控特定字段的更改。在上述情况下,$watch函数的第一个参数中的attributes.chart将被监视,第二个参数用于实际检查修改后的数据并对其执行操作。
您还可以在官方的angular docs中找到$watch可以使用的更多选项:$rootScope.Scope#$watch$watch监控模型更改,一旦模型更改、更新值从下面的代码中获得,并且根据需要,可以执行所需的操作 $scope.$watch('ng-model-name',函数(newval,oldval){ if(newval<0){
$('#modelCustomer').model('show');
}
}); 因此,对于这篇文章,如果attributes.chart有任何更改,它将调用下一个函数来评估attributes.chart值的更改?