相关疑难解决方法(0)

将D3.js与Angular 2一起使用

我已成功将Angular 2(Alpha 44)与D3.js集成:

<html>
<head>
<title>Angular 2 QuickStart</title>
<script src="../node_modules/systemjs/dist/system.src.js"></script>
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script>
  System.config({packages: {'app': {defaultExtension: 'js'}}});
  System.import('app/app');
</script>
</head>
<body>
<my-app>Loading...</my-app>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

app.js:

/// <reference path="./../../typings/tsd.d.ts" />

import {Component, bootstrap, ElementRef} from 'angular2/angular2';

@Component({
  selector: 'my-app',
  template: '<h1>D3.js Integrated if background is yellow</h1>',
  providers: [ElementRef]
})
class AppComponent { 
  elementRef: ElementRef;

  constructor(elementRef: ElementRef) {
   this.elementRef = elementRef;
  }

afterViewInit(){
    console.log("afterViewInit() called");
    d3.select(this.elementRef.nativeElement).select("h1").style("background-color", "yellow");
  }
}
bootstrap(AppComponent);
Run Code Online (Sandbox Code Playgroud)

一切都很好.但是ElementRef的Angular 2文档说明如下:

当需要直接访问DOM时,请使用此API作为最后的手段.请使用Angular提供的模板和数据绑定.或者,您可以查看{@link Renderer},它提供了可以安全使用的API,即使不支持直接访问本机元素也是如此.依赖于直接DOM访问会在应用程序和渲染层之间产生紧密耦合,这使得无法将两者分开并将应用程序部署到Web工作者中.

现在问题是如何将D3.js与Renderer API集成?

d3.js angular

75
推荐指数
2
解决办法
4万
查看次数

Angular 2:如何从组件中控制<video>

我正在尝试使用Angular 2,但冻结处理HTML 5视频.在我发现的手册中,可以在模板中使用:

<video #videoplayer></video>
Run Code Online (Sandbox Code Playgroud)

这应该创建局部变量"videoplayer",提供对视频元素的访问.可以从组件中获取它,例如:

@Component({
    selector: '<some>',
    template: '<video #videoplayer></video>'
})

export class SomeComponent {

   public videoplayer; // How can I got element object here?

   play() { // I need work with <video> in Component
      this.videoplayer.play();
   }
}
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助,我尝试@Host它或像@Input一样使用它但仍然无法正常工作.

angular

4
推荐指数
1
解决办法
4822
查看次数

标签 统计

angular ×2

d3.js ×1