6
我正在尝试整合d3和angular2 alpha.37(从here开始)。我目前遇到的问题是生成的DOM元素不能获得仿真样式视图封装中使用的属性,所以我无法设置它们的样式,而没有将该元素的视图封装设置为无(或本机,但我宁愿使用模拟)。添加主机属性到生成的DOM元素
我设法以编程提取的元件所需的属性分量[1]内,然后将其添加到所生成的元素[2],它不工作,但这显然难以置信哈克:
import {Component, View, Attribute, ElementRef, LifecycleEvent} from 'angular2/angular2';
import d3 from 'd3';
@Component({
selector: 'bar-graph',
properties: [ 'data' ]
})
@View({
template: '<div class="chart"></div>',
styles: [`.chart {
background: #eee;
padding: 3px;
}
div.bar {
width: 0;
transition: all 1s ease-out;
-moz-transition: all 1s ease-out;
-webkit-transition: all 1s ease-out;
}
div.bar {
font: 10px sans-serif;
background-color: steelblue;
text-align: right;
padding: 3px;
margin: 5px;
color: white;
box-shadow: 2px 2px 2px #666;
}`]
})
export class BarGraph implements LifecycleEvent.OnChanges {
data: Array<number>;
divs: any;
constructor(elementRef: ElementRef, @Attribute('width') width: string, @Attribute('height') height: string) {
var el:any = elementRef.nativeElement;
var graph:any = d3.select(el);
this.hostAttr = graph[0][0].children[0].attributes[1].name; //hack here [1]
this.divs = graph.
select('div.chart').
style({
'width': width + 'px',
'height': height + 'px',
}).
selectAll('div.bar');
}
render(newValue) {
if (!newValue) return;
this.divs.data(newValue)
.enter().append('div')
.classed('bar', true)
.attr(this.hostAttr, true) //add the attribute here [2]
.style('width', d => d + '%')
.text(d => d + '%');
}
onChanges() {
this.render(this.data);
}
}
是否有推荐的方式来处理这类事情(或者我应该停止修改Angular2之外的DOM)?
如果您想要一个答案,您至少必须升级到beta.0。 –
@EricMartinez在beta.7上有同样的问题。 –