【发布时间】:2017-07-14 23:19:10
【问题描述】:
我正在尝试将异步数据传递给子 d3 组件以绘制图表。
parentComponent.controller = function(){
this.data = null;
this.$postLink = function () {
console.log('postlink')
async(url).then(function(data){
this.data = data;
}.bind(this))
}
}
childComponent({
bindings: {
data: '<'
},
controller: function(){
this.$onInit = function () {
console.log('init')
console.log(this.data)
}
this.$onChanges = function () {
console.log('onchange')
console.log(this.data)
}
this.$doCheck = function () {
console.log('docheck')
console.log(this.data)
}
}
})
日志如下所示:
onchange null
init null
docheck null
postlink
但如果我同步更改父数据,它会起作用。
this.$postLink = function () {
console.log('postlink')
this.data = 'new data';
}
// logs
.
.
.
postlink
docheck 'new data'
onchange 'new data'
谁能告诉我为什么这不起作用以及如何制作 d3 角度组件?
【问题讨论】:
-
什么是异步()?如果您“在其背后”修改模型,即不使用 $timeout、$interval 或 $http 之类的服务之一,Angular 不会知道模型已更改。在这种情况下,您需要调用 $scope.$apply()。
-
您的 html 结构如何将数据传递给子组件?请也粘贴那个
标签: angularjs asynchronous d3.js