Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带动画的Angular 4 ComponentFactoryResolver?_Javascript_Angular_Angular Components_Angular Animations - Fatal编程技术网

Javascript 带动画的Angular 4 ComponentFactoryResolver?

Javascript 带动画的Angular 4 ComponentFactoryResolver?,javascript,angular,angular-components,angular-animations,Javascript,Angular,Angular Components,Angular Animations,我尝试创建一种Viewcontroller服务,它动态创建(并销毁)组件(类似于Viewcontroller)。在新组件中添加一些滑动或淡入淡出的动画会很好,但我还不知道如何做到这一点。我知道角度路由器,它允许动画,但我更喜欢不使用路由器的解决方案 这是我得到的,到目前为止效果很好 @Injectable() export class ViewControllerService { constructor( private zone: NgZone, private resolver: C

我尝试创建一种Viewcontroller服务,它动态创建(并销毁)组件(类似于Viewcontroller)。在新组件中添加一些滑动或淡入淡出的动画会很好,但我还不知道如何做到这一点。我知道角度路由器,它允许动画,但我更喜欢不使用路由器的解决方案

这是我得到的,到目前为止效果很好

@Injectable()
export class ViewControllerService {

constructor( private zone:  NgZone, private resolver: 
ComponentFactoryResolver, private injector: Injector, private 
appRef:ApplicationRef) { 

}

myComponentRef: ComponentRef<MyComponent>;
createMyComponent(data){

this.zone.run( () => {

  if(this.myComponentRef){
    this.myComponentRef.destroy();
  }
  const compFactory = this.resolver.resolveComponentFactory(MyComponent);
  this.myComponentRef = compFactory.create(this.injector);

  //set data to component Input
  this.myComponentRef.instance.data = data;

  //listen to EventEmitter dismiss in component
  this.myComponentRef.instance.dismiss.subscribe(data => { 
    console.log("closing component",data)
    if(this.myComponentRef){
      this.myComponentRef.destroy();

    }
  });

  if (this.appRef['attachView']) { // since 2.3.0
    this.appRef['attachView'](this.myComponentRef.hostView);
    this.myComponentRef .onDestroy(() => {
      console.log("attachView onDestroy",this.myComponentRef)
        this.appRef['detachView'](this.myComponentRef.hostView);

    });
  } else {
    this.appRef['registerChangeDetector'](this.myComponentRef.changeDetectorRef);
    this.myComponentRef.onDestroy(() => {
      console.log("onDestroy",this.myComponentRef)
      this.appRef['unregisterChangeDetector'](this.myComponentRef.changeDetectorRef);

    });
  }

  //attach component to base element
  let div = document.createElement('div');
  div.setAttribute("class","my-class")
  div.appendChild(this.myComponentRef.location.nativeElement)
  document.getElementById("base").appendChild(div);


})
}
但是如何将动画附加到新创建的组件?直接在组件本身内部?或者有没有一种使用ComponentFactoryResolver的方法?遗憾的是,角度渲染器在服务中无法工作

export const slideInOutAnimation = [
trigger('slideInOut', [
state('in', style({
  transform: 'translate3d(0, 0, 0)'
})),
state('out', style({
  transform: 'translate3d(100%, 0, 0)'
})),
transition('in => out', animate('400ms ease-in-out')),
transition('out => in', animate('400ms ease-in-out'))
]),
]