ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后发生了变化

时间:2017-06-29 09:11:16

标签: angular

我尝试将值设置为我的组件时出现此错误。 这是我的组成部分:

import { Component, OnChanges, Input } from '@angular/core';
import { DataService } from '../services/data.service';
import { ColorService } from '../services/color.service';
import { ChartsComponent } from '../charts/charts.component';
import * as _ from 'lodash';

@Component({
  selector: 'app-overview',
  templateUrl: './overview.component.html',
  styleUrls: ['./overview.component.scss']
})
export class OverviewComponent extends ChartsComponent  {

  public candle_ChartData = [];
  private viewLoaded = false;

  constructor(private colorService: ColorService) { 
    super();
  }

  showGraph() {
    if (this.candle_ChartData !== undefined &&
        this.candle_ChartData.length > 0 ){
      return true;
    } 
    return false;
  }

  getHeader() {
    if (this.selectedSymbol !== undefined ) {
      return this.selectedSymbol.name + " - " + this.selectedSymbol.lastPrice + this.selectedSymbol.currency;
    }
    return "";
  }

  buildChart() {
    this.candle_ChartData = [];
    this.candle_ChartData.push(['Day', 'Low', 'Opening value', 'Closing value', 'High']); 

    let temp_price = this.price;
    if (this.price.length > 0) {
      if (this.price.length > 100) {
        temp_price = _.drop(this.price, this.price.length - 100);
      }
    }

    for (let value of temp_price) {
      let date = new Date(value.time);
      let dateString
      this.candle_ChartData.push([date, value.low, value.open, value.close, value.high]);
    }
    console.log(this.candle_ChartData);
  };


  public candle_ChartOptions = {
    legend: 'none',
    bar: { groupWidth: '100%' }, // Remove space between bars.
    colors: ['#000'],
    candlestick: {
      fallingColor: { strokeWidth: 0, fill: '#D32F2F' }, // red
      risingColor: { strokeWidth: 0, fill: '#00796B' } // green
    },
    chartArea: {
                    left: 80,
                    right: 10, // !!! works !!!
                    bottom: 80, // !!! works !!!
                    top: 20,
                    width: "100%",
                    height: "100%"
                },
      interpolateNulls: true

  };

  toggleTheme() {
    this.colorService.toggleTheme(!this.colorService.isDarkTheme());
  }

  isDarkTheme() {
    return this.colorService.isDarkTheme();
  }
}

这是它的扩展类:

import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { Price } from '../shared/price';
import { Symbol } from '../shared/Symbol';
import { ChartInterface } from '../interface/chart-interface';

@Component({
  selector: 'app-charts',
  templateUrl: './charts.component.html',
  styleUrls: ['./charts.component.scss']
})
export class ChartsComponent implements ChartInterface, OnChanges  {
  @Input() price: Price[];
  @Input() selectedSymbol: Symbol;

  constructor() { }

  update(price: Price[], selectedSymbol: Symbol) {
      if (price !== undefined && selectedSymbol !==undefined ) {
        this.price = price;
        this.selectedSymbol = selectedSymbol;
        this.buildChart();     
      }
  }

  ngOnChanges(changes: SimpleChanges) {
    if (changes.price !== undefined && 
        changes.price.currentValue !== undefined && 
        changes.price.currentValue.length > 0 ) {
        this.buildChart();     
    }
  }

  buildChart() {
    console.log("asdsassdaads");
  }

}

最后这就是我设置值的方式:

let factory = this.componentFactoryResolver.resolveComponentFactory(this.type);
this.cmpRef = this.target.createComponent(factory);
(<ChartsComponent>this.cmpRef.instance).update(this.price, this.selectedSymbol);

我一直在四处搜寻,但我并不是真的了解错误。 我可以通过某种方式强制onChange或类似的东西来传递它吗?

BR

0 个答案:

没有答案