gpt4 book ai didi

javascript - $watch on 指令,其嵌套对象的隔离范围作为 crossfilter 的属性

转载 作者:行者123 更新时间:2023-12-03 04:36:11 24 4
gpt4 key购买 nike

我创建了一个具有隔离范围、元素的指令,并使用传入的这些值来构建 d3/dc 图表。所以我有一些数据通过 $scope 上的 crossfilter 放置,以便指令属性可以读取它。然后我拉入指令,日志记录范围显示它很好,但是我无法在没有未定义的情况下专门/直接访问嵌套对象。

在 Controller 内:

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 等,但似乎没有什么能让我获得该嵌套对象。

谁能帮我理解这里发生了什么?

最佳答案

尝试使用$watch

link: function(scope, elem, attr) {      
scope.$watch('dimension', function(nVal, oVal) {
if (nVal) {
console.log('dimension=', scope.dimension);
}
});
}

关于javascript - $watch on 指令,其嵌套对象的隔离范围作为 crossfilter 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43293765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com