我有一個使用d3顯示項目的角度應用程序,並且有一個顯示被點擊項目信息的infoPanel。這是工作,但點擊該項目來激活/打開infopanel不起作用。
在D3數據控制文件,我有:
'use strict';
var settings ;
angular.module('App')
.directive('data', ['sessionmgmt', 'thedata', 'gui', function (session, data, gui) {
return {
link: function postLink(scope, element, attrs) {
var promise = angular....getData...;
promise.then(function() {
settings = session.settings;
svg = ...
createItems(svg, items);
}, function() {});
}
};
}]);
function createItems(svg, items) {
let item = svg.append("g")
.attr("class", "nodes")
.selectAll(".node")
.data(items, function (d) {
return d.id;
});
appendIcons(item);
}
function appendIcons(nodes){
nodes = nodes.enter().append("g")
.attr("class", "node");
nodes.append("title")
.text(function (d) {
return d.data['label'];
});
nodes.on("click",function(d){
if(settings.selectedItem === d.id){
settings.selectedItem = null;
}else{
settings.selectedItem = d.id;
}
});
}
然後在我的信息面板控制器我有:
'use strict';
angular.module('App')
.controller('ContentpanelCtrl', ['$scope', 'sessionmgmt', function ($scope, sessionmgmt) {
var contentPanel = this;
this.settings = sessionmgmt.settings;
$scope.activate = function activate() {
contentPanel.settings.contentPanelOpen = true;
};
$scope.getSelected = function getSelected(){
return contentPanel.settings.itemSelected;
}
$scope.isNodeSelected = function isSelected(){
return contentPanel.settings.itemSelected !== null;
}
$scope.$watch("contentPanel.settings.itemSelected", function(newVal, oldVal) {
console.log(newVal);
if (newVal !== oldVal){
$scope.activate();
}
});
}]);
的執行console.log運行時,第一次加載頁面,但那麼當我點擊物品時,這隻手錶並未被解僱。 我知道'click'正在工作,因爲getSelected正在我的infopanel上顯示正確的數據。 '手錶'和getSelected都使用完全相同的設置變量....難住。
添加點擊功能在您的控制器,並添加'NG-click'in你對一個項目的HTML指向該功能。我不認爲在你使用jQuery以外的情況下,angular會發現這種變化。 – Arg0n
您是否嘗試將範圍傳遞給'createItems'然後'appendIcons'函數並在點擊事件後調用'scope。$ apply()'? – Razzildinho
@Razzildinho - 那真是太棒了!謝謝。我仍然沒有使用$ watch函數,但是不需要,因爲我的click函數現在運行'contentPanel.settings.contentPanelOpen = true;'和scope。$ apply使它可見。 –