2017-10-07 165 views
0

我想降级角2指令与Angular 1.5应用程序一起使用。 我们能够降级Angular 2组件,但不能降级指令。角度2降级指令不工作

import { Directive, ElementRef, Input, Renderer } from '@angular/core'; 
    import { UpgradeComponent } from '@angular/upgrade/static'; 

    @Directive({ 
     selector: 'myDir' 
    }) 
    export class MyDirective { 
     constructor(public el: ElementRef, public renderer: Renderer) { 
      console.log('constructor'); 
     } 
     ngOnInit() { 
      console.log("onInit"); 
     } 
    } 

    angular.module(myapp) 
      .directive('myDir', downgradeComponent({ component: MyDirective })); 

当我尝试在Angular 1应用程序中使用指令时,它不会触发构造函数或ngOnInit。

这就是我如何使用它。

<myDir></myDir> 

回答

1

不能从角2降级@Directive到角1.我们挣扎着类似的问题,我们解决了它在不那么漂亮的方式。我们要降级@Component与NG含量为模板,然后包裹元素与它(治标不治本,一旦所有在角2,我们重构它)

,所以我们将不得不

@Component({ 
     selector: 'myDir', 
     template: '<ng-content></ng-content>' 
    }) 

这就是我们如何应用它

<myDir><someOtherElementsWhereDirectiveShouldBeApplied></myDir> 
+0

谢谢!你的方法是否也适用于属性指令?说如果我们需要

user636525

+0

不幸的是,角降级只支持组件作为元素而不是属性 – pezetem