求助:关于angular2+ @Input有一处不理解

1. 问题,不太理解ngx-bootstrap示例中的的@Input使用方法

hero-parent.component.ts

@Component({
  selector: 'app-hero-parent',
  template: `
    <h2>{{master}} controls {{heroes.length}} heroes</h2>
    <app-hero-child *ngFor="let hero of heroes"
      [hero]="hero"
      [master]="master">
    </app-hero-child>
  `
})
export class HeroParentComponent {
  heroes = HEROES;
  master = 'Master';
}

hero-child.component.ts

@Component({
  selector: 'app-hero-child',
  template: `
    <h3>{{hero.name}} says:</h3>
    <p>I, {{hero.name}}, am at your service, {{masterName}}.</p>
  `
})
export class HeroChildComponent {
  @Input() hero: Hero;
  @Input('master') masterName: string;
}

ngx-bootstrap中Alert组件部分@Input的使用:

demo/.../alerts/.../basic/basic/html:

<alert type="success" test="123">
    <strong>Well done!</strong> You successfully read this important alert message.
</alert>

src/alert/alert.components.ts:

@Component({
  selector: 'alert,bs-alert',
  templateUrl: './alert.component.html'
})
export class AlertComponent implements OnInit {
  @Input() type = 'warning';
  ...
  ngOnInit(): void {
    console.log('type:'+ this.type);
    ...
  }
  ...
}

2. 疑问: 当我尝试去把ngx-bootstrap中base.html处的type="warning"修改为[type]="warning"时,获取不到值,控制台打印undefined。

阅读 2.2k
1 个回答

知道结果了:
如果属性不添加[]则angular认为其值为一个不会改变的字符串,如果添加[]则angular认为其值为变量。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进