简体   繁体   English

从类更改组件装饰器Angular 2的属性

[英]Change properties at component decorator Angular 2 from class

I want to change template after some action happens 我想在一些动作发生后更改模板

@Component({
  ... 
  template: this.myTemplate
})

export class App {
  myTemplate: = 'Here is first template';

  // Replace templates
  public changeTempate() {
    myTemplate = 'Here is second template';
  }

How to make this code work? 如何使这段代码工作?

Finally It was made in Angular 2.3.0 Here is simple sample: 最后它是在Angular 2.3.0中制作的。这是一个简单的例子:

//our root app component
import {Component, NgModule, Input, Output, EventEmitter, HostBinding, HostListener} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

@Component({
  selector: 'person',
  //template will NOT be inherited 
  template: `<h4>Person: {{name}}</h4>`
})
export class Person {
  @Input() name; //will be inherited
  //  @Output() onClick = new EventEmitter(); //will be inherited
  //  clicked() { //will be inherited
  //    this.onClick.emit(this.name);
  //  }
  @HostBinding('style.color') color = 'red';  //will be inherited

  @HostListener('click', ['$event']) //will be inherited 
  onClick(e){
    console.log(this.name);
  }
}

@Component({
  selector: 'employee',
  template: `
    <h4>Employee: {{name}}, id: {{id}}</h4>
  `
})
export class Employee extends Person {
  @Input() id; // new

  @HostListener('click', ['$event']) // overridden
  onClick(e) { 
    console.log(`${this.name}-${this.id}`);
  }
}


@Component({
  selector: 'my-app',
  template: `
    <div>
      <person name="John"></person>
      <employee name="Tom" id="45231"></employee>
    </div>
  `,
})
export class App { }

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App, Person, Employee ],
  bootstrap: [ App ]
})
export class AppModule {}

For more information please read 欲了解更多信息,请阅读

https://medium.com/@gerard.sans/angular-2-new-features-in-angular-2-3-f2e73f16a09e#.33fk3molt https://medium.com/@gerard.sans/angular-2-new-features-in-angular-2-3-f2e73f16a09e#.33fk3molt

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

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