在点击星星时,无法重新执行ngOnChanges.

来源:6-11 本章实战

Maybrittnels4034018

2017-08-02

stars.component.ts:
import {Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges} from '@angular/core';

@Component({
  selector: 'app-stars',
  templateUrl: './stars.component.html',
  styleUrls: ['./stars.component.css']
})

export class StarsComponent implements OnInit, OnChanges {


  @Input()
  rating = 0;

  @Output()
  ratingChage: EventEmitter<number> = new EventEmitter();


  stars: boolean[];

  @Input()
  readonly: boolean;


  constructor() { }

  ngOnInit() {
  }

  ngOnChanges(changes: SimpleChanges): void {
    this.stars = [];
    for (let i = 1; i <= 5; i++) {
      this.stars.push(i > this.rating);
    }
  }

  clickStart(index: number) {
    if (this.readonly) {
      this.rating = index + 1;
      this.ratingChage.emit(this.rating);
    }
  }

}

stock-form.component:
<app-stars [(rating)]="stock.rating" [readonly]="true" ></app-stars>


写回答

1回答

seeley114

2017-08-03

ratingChange单词写错了。

0
2
Maybrittnels4034018
非常感谢!
2017-08-03
共2条回复

Angular4.0从入门到实战 打造股票管理网站

Angular新特性,教你熟练使用 Angular 和 Typescript 开发组件式单页应用

2683 学习 · 1361 问题

查看课程