【问题标题】:downgrade directive component from angular 2 to angularJS is not working as expected从 angular 2 降级到 angularJS 的指令组件未按预期工作
【发布时间】:2017-09-30 00:36:08
【问题描述】:

这是我的 angular 2 组件:

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

...

这允许我在 Angular 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 元素(指令)的指令。

【问题讨论】:

    标签: angular angular2-directives angular2-components


    【解决方案1】:

    我遇到了同样的问题,并在以下位置找到了一个词:https://github.com/angular/angular/issues/16695#issuecomment-336456199

    【讨论】:

      【解决方案2】:

      尝试使用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

      【讨论】:

      • 我得到 downgradeNg2Component 在 upgradeAdapter 的类型上不存在
      • 您是从'angular2/upgrade' 导入UpgradeAdapter 吗?并且还将upgradeAdapter 声明为new UpgradeAdapter()?
      • 从'@angular/upgrade'导入{UpgradeAdapter};
      • 不,新的 UpgradeAdapter 说:提供的参数不匹配任何签名或调用目标
      • 我更新了我的答案。请记住,您必须导入组件,然后将其作为参数传递到downgradeNg2Component()。希望对人有所帮助,告诉我! :D
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 2013-05-03
      • 1970-01-01
      相关资源
      最近更新 更多