2

Is exportAs property defined for a component. 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')
  }
}
5
  • That should work. What is the problem? Commented Apr 29, 2016 at 20:33
  • Cannot see the log. No error is shown Commented Apr 30, 2016 at 5:40
  • 1
    why do you need exportAs ? You can do the same thing by just writing #my instead of #my="myApp" Commented Apr 30, 2016 at 8:16
  • here it is an example of my problem. plnkr.co/edit/PAudXulH5VzecE9fYT1l?p=preview as you could see the button outside the component does not work ... Commented Apr 30, 2016 at 9:13
  • exportAs is used for directives, see: plnkr.co/edit/IlLtBY7Ic9yKiRIpjukf?p=preview Commented Oct 5, 2016 at 16:56

3 Answers 3

3

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).

You might be looking for something like How to dynamically create bootstrap modals as Angular2 components?

Sign up to request clarification or add additional context in comments.

Comments

1

exportAs is used for directives, see: 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", "");
  }
}

Comments

0

exportAs was introduced, and has been available from Angular 5.

Angular 2,4 dont have it. Here is the link to Angular.io blog for Angular5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.