我将 D3 svg 图像包装在角度指令中。当用户单击该 D3 图像时,我想在控制器中将变量设置为 true,以便 ng-show 然后显示另一个 D3 图像。
我所做的是.on("click")
向 D3 图像添加一个函数,并在该函数中用于$rootScope.$emit()
发送事件。在第二张图像的控制器中,我有一个$rootScope.$on()
ctach 事件并将 ng-show 的变量设置为 true。
这种方法行不通。我已经测试了代码以确保事件被正确地发出和捕获,但是 ng-show 没有显示第二个 D3 图像。
为什么是这样?
我创建了一个小 plunkr 来说明我正在尝试做的事情。 http://plnkr.co/edit/FnqeAF9kVXxdUdOzT5to。
控制器代码如下:
function CircleCtrl($rootScope) {
this.render = function(element, attrs) {
var svg = d3.select(element[0]).append("svg")
.attr("width", 200)
.attr("height", 200);
var circle = svg.append("circle")
.attr("cx", 30)
.attr("cy", 30)
.attr("r", 20);
circle.on("click", function(d,i) {
var data = {val0: "zero", val1: "one"};
$rootScope.$emit("Circle Clicked", data);
});
};
}
function SquareCtrl($rootScope) {
this.showSquare = false;
$rootScope.$on("Circle Clicked", function(event, data) {
this.showSquare = true;
})
this.render = function(element, attrs) {
var svg = d3.select(element[0]).append("svg")
.attr("width", 200)
.attr("height", 200);
var rectangle = svg.append("rect")
.attr("x", 10)
.attr("y", 10)
.attr("width", 50)
.attr("height", 100);
};
}
angular.module("AngularD3Example")
.controller("CircleCtrl", ["$rootScope", CircleCtrl])
.controller("SquareCtrl", ["$rootScope", SquareCtrl]);