Angularjs 角度+;D3-从单击事件获取要在模板中显示的范围值
我正在使用D3和Angular开发一个地图应用程序,在模板中显示点击事件的范围值时遇到了一个问题 我的指示:Angularjs 角度+;D3-从单击事件获取要在模板中显示的范围值,angularjs,d3.js,angular-directive,Angularjs,D3.js,Angular Directive,我正在使用D3和Angular开发一个地图应用程序,在模板中显示点击事件的范围值时遇到了一个问题 我的指示: angular.module("maineMap") .directive("ngMap", ["appConfig", function(config){ function countyClickHandler(d, i, scope){ scope.currentCounty(d.properties.fips);
angular.module("maineMap")
.directive("ngMap", ["appConfig", function(config){
function countyClickHandler(d, i, scope){
scope.currentCounty(d.properties.fips);
console.log(scope.countySelection);
}
function initMap() {
//.... other code
g.append("g")
.attr("class", "counties")
.selectAll("path")
.data(scope.mapData.features)
.enter()
.append("path")
.attr("d", path)
.attr("class", function(d){
return d.properties.name;
})
.on("click", function(d, i){
countyClickHandler(d, i, scope);
});
}
return {
restrict : "E",
link : function(scope, el, attrs){
initMap(scope, el);
}
};
}]);
我的控制器:
angular.module("maineMap")
.controller('MapCtrl', ['$scope','MapService', function($scope, MapService){
//execute data fetches via promises on template load
$scope.mapData = MapService.getMapPaths().getData();
$scope.cityData = MapService.getCityPositions().getData();
$scope.countyData = MapService.getMapData().getData();
$scope.countySelection = {};
$scope.currentCounty = function(fips){
$scope.countySelection = getCountyData($scope, fips);
console.log($scope.countySelection);
};
}]);
//helper function to extract data from external JSON file
function getCountyData($scope, fips){
for(var i = 0; i < $scope.countyData.properties.length; i++) {
if ($scope.countyData.properties[i].FIPS === fips){
return $scope.countyData.properties[i];
}
}
}
countyData
div的唯一输出是County元素的一对空{}
countySelection.POP2000
为空
鉴于此布局,如何通过
单击事件
更新模板的作用域值?解决方案是在单击处理程序中使用$apply
:
function countyClickHandler(d, i, scope){
scope.$apply(function(){
scope.currentCounty(d.properties.fips);
});
}
function countyClickHandler(d, i, scope){
scope.$apply(function(){
scope.currentCounty(d.properties.fips);
});
}