我创建了一个带有隔离范围,元素和am的指令,使用传入的这些值来构建d3 / dc图表。所以我在$ scope上有一些通过crossfilter放入的数据,因此指令属性可以读取它。然后我进入指令并且日志记录范围显示它很好,但是我不能专门/直接访问嵌套对象而不会得到未定义。
控制器内的:
var data = [{
"player": "Player A",
"team": "New York",
"hits": 200
}, {
"player": "Player B",
"team": "New York",
"hits": 225
}, {
"player": "Player C",
"team": "New York",
"hits": 1
}, {
"player": "Player D",
"team": "San Francisco",
"hits": 268
}, {
"player": "Player E",
"team": "San Francisco",
"hits": 22
}, {
"player": "Player F",
"team": "Seattle",
"hits": 2
}, {
"player": "Player G",
"team": "Seattle",
"hits": 25
}]
$scope.ndx = crossfilter(data);
$scope.playerDim = $scope.ndx.dimension(function (d) {
return d.player;
});
$scope.playerGrp = $scope.playerDim.group().reduceSum(function (d) {
return d.hits;
});
HTML
<my-chart id="someChart"
chart-type="pieChart"
height="200"
width="200"
dimension="playerDim"
group="playerGrp" />
指令:
myApp.directive('myChart', ['chartFactory' function(chartFactory) {
return {
restrict: 'E',
scope = {
chartType: "@",
height: "@",
width: "@",
dimension: "=",
group: "="
},
link: function(scope, elem, attr) {
console.log(scope) // shows the dimension object as a property on the object just fine
console.log(scope.dimension) // undefined
}
};
}])
我的猜测是,当我在console.log中时,该函数并没有真正设置对象,尽管它在那里显示(误导)。我已经尝试过$ watch,$ timeout等等,似乎没有什么能让我得到那个嵌套对象。
有人可以帮我理解这里发生了什么吗?
答案 0 :(得分:1)
尝试使用$watch
link: function(scope, elem, attr) {
scope.$watch('dimension', function(nVal, oVal) {
if (nVal) {
console.log('dimension=', scope.dimension);
}
});
}