简体   繁体   English

尝试在 angular2 组件中使用 exportAs

[英]try to use exportAs in angular2 component

Is exportAs property defined for a component.是为组件定义的 exportAs 属性。 How can I access outside the component to a method of it?如何在组件外部访问它的方法? I tried this example我试过这个例子

<my-app #my="myApp">
loading...
</my-app>


<button (click)="my.displayMessage()" class="ui button">
  Display popup for message element
</button>

Here the component class这里的组件类

 import {Component} from 'angular2/core'

 @Component({
 selector: 'my-app',
 providers: [],
 template: `
  <div>
    <h2>Hello {{name}}</h2>

  </div>
  `,
  directives: [],
   exportAs: "myApp"
})
export class App {
  constructor() {
   this.name = 'Angular2'
  }

  displayMessage():void {
   console.log('called from component')
  }
}

That is not supposed to work.那不应该工作。 You can't have any #xxx , (xxx) or any other kind of Angular binding outside the template of you root component ( App ).在根组件 ( App ) 的模板之外,您不能有任何#xxx(xxx)或任何其他类型的 Angular 绑定。

You might be looking for something like How to dynamically create bootstrap modals as Angular2 components?您可能正在寻找类似How to dynamic create bootstrap modals as Angular2 components?

exportAs is used for directives, see: http://plnkr.co/edit/IlLtBY7Ic9yKiRIpjukf?p=preview exportAs 用于指令,请参阅: http ://plnkr.co/edit/IlLtBY7Ic9yKiRIpjukf?p=preview

@Directive({
  selector: "div",
  exportAs: "myDiv"
})
class MyDiv {

  constructor(private element: ElementRef, private renderer: Renderer) {
  }

  toUpper() {
    return this.renderer.setElementStyle(this.element.nativeElement, "text-transform", "uppercase");
  }

  toLower() {
    return this.renderer.setElementStyle(this.element.nativeElement, "text-transform", "lowercase");
  }

  reset() {
    return this.renderer.setElementStyle(this.element.nativeElement, "text-transform", "");
  }
}

exportAs was introduced, and has been available from Angular 5. exportAs被引入,并且在 Angular 5 中可用。

Angular 2,4 dont have it. Angular 2,4 没有。 Here is the link to Angular.io blog for Angular5这是Angular5 的 Angular.io 博客的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM