我使用Angularjs組件和ES6構建一個也使用d3js的應用程序。我正在使用DOM操作的屬性指令。由於該組件具有我試圖在鏈接函數中訪問它的範圍,並在內部使用$ watch。我的疑問來自於我試圖觀察和訪問的變量在$ scope中。$ ctrl。在這種情況下訪問它們的正確方法是什麼?
模板
<div class="card">
<svg network-svg>
</svg>
</div>
組件
export const NetworkComponent = {
template,
controller: class NetworkComponent {
constructor(CommService) {
'ngInject'
this.comm = CommService;
this.physicalTree = new NodeTree('physical');
this.virtualTree = new NodeTree('virtual');
this.pVisible = true;
}
}
}
指令 現行指令的代碼,但返回undefined。
export const NetworkSvg =() => {
return {
restrict: 'A',
link($scope, $element, $attrs) {
$scope.$watch('virtualTree', function() {
});
$scope.$watch('physicalTree', function() {
});
}
}
因此,在使用$ scope的情況下訪問它並沒有什麼壞處。$ ctrl.physicalTree? –
查看我的答案更新。 – georgeawg
非常感謝,那正是我對此的懷疑,似乎有點做作。但我看到屬性是要走的路。 –