2017-05-02 50 views
3

這裏是我的角2部分:降級指令組件無法正常運行

@Component({ 
    selector: '[my-component]', 
    template:`<ng-content></ng-content>` 
}) 
export class MyComponentClass { 
    @Input() myComponent: string; 

... 

這讓我在角2應用程序上的任何HTML標記以此爲指令,例如:

<div [myComponent]="'my text string'"></div> 
<span [myComponent]="'my text string'"></span> 

但取相同的部件降級時在angularJs應用使用:

angular.module('myApp') 
    .directive('myComponent', downgradeComponent({component: MyComponentClass , inputs: ['myComponent']}) as angular.IDirectiveFactory); 

現在,這是行不通的:

<div [myComponent]="'my text string'"></div> // or 
<div [my-component]="'my text string'"></div> 

只有當我把它作爲像

<my-component [my-component]="'some text'"></my-component> 

我做了什麼不對的組成部分? 我不希望它被用作新的html標籤,而是作爲可以添加到任何html元素(指令)的指令。

回答

0

嘗試使用downgradeNg2Component代替downgradeComponent

import {UpgradeAdapter} from 'angular2/upgrade'; 
import {myComponent} from './my-component.component.ts'; 

let upgradeAdapter = new UpgradeAdapter(); 

.directive('myComponent', 
    upgradeAdapter.downgradeNg2Component(myComponent)) 

入住這useful post

+0

我得到downgradeNg2Component不存在於類型upgradeAdapter – AngularOne

+0

是您導入'UpgradeAdapter'從''angular2/upgrade''?還聲明'upgradeAdapter'爲'新的UpgradeAdapter()'? – SrAxi

+0

從'@ angular/upgrade'導入{UpgradeAdapter}; – AngularOne