简体   繁体   English

Angular2使用ApplicationRef手动实现更改检测

[英]Angular2 manually implement change detection using ApplicationRef

Getting a change detection error 获得更改检测错误

Expression has changed after it was checked. 检查后表情发生了变化。 Previous value: 'true'. 上一个值:'true'。 Current value: 'false' 当前价值:'假'

so I want to manually run another round of change detection. 所以我想手动运行另一轮变化检测。 Found information about using ApplicationRef.tick() but currently getting error 找到有关使用ApplicationRef.tick()但当前收到错误的信息

ERROR in [default] C:\\development\\SolarUI11\\src\\app\\update\\update.component.ts:8 :11 Argument of type '{ selector: string; ERROR在[默认] C:\\ development \\ SolarUI11 \\ src \\ app \\ update \\ update.component.ts:8:11类型'{selector:string; styles: any[]; 风格:任何[]; template: any; 模板:任何; providers: ( typeof ApplicationRef | typeof Date...' is not assignable to parameter of type ' Component'. Types of property 'providers' are incompatible. Type '(typeof ApplicationRef | typeof DatePipe)[]' is not assignable to type 'Provider[]'. Type 'typeof ApplicationRef | typeof DatePipe' is not assignable to type ' Provider'. Type 'typeof ApplicationRef' is not assignable to type 'Provider'. Type 'typeof ApplicationRef' is not assignable to type 'FactoryProvide r'. Property 'provide' is missing in type 'typeof ApplicationRef'.` 提供者:( typeof ApplicationRef | typeof Date ...'不能分配给'Component'类型的参数。属性'provider'的类型是不兼容的.Type'(typeof ApplicationRef | typeof DatePipe)[]'不能赋值给' Provider []'。类型'typeof ApplicationRef | typeof DatePipe'不能分配给'Provider'类型。类型'typeof ApplicationRef'不能分配给'Provider'类型。类型'typeof ApplicationRef'不能分配给'FactoryProvide r'类型类型'typeof ApplicationRef'中缺少属性'provide'

I think I'm just stuck on the syntax of implementing this, couldn't find enough information myself to be able to use it. 我想我只是坚持实现这个的语法,自己找不到足够的信息才能使用它。

Typescript: 打字稿:

import { Component, Input, ApplicationRef } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import {NgbDateStruct} from '@ng-bootstrap/ng-bootstrap';
import {DatePipe} from "@angular/common";
import { DataTable } from '../data/datatable';
import { DPS } from '../data/datainfo.ts';

@Component({
  selector: 'update-validation',
  styleUrls: ['../app.component.css'],
  templateUrl: 'update.component.html',
  providers: [DatePipe, ApplicationRef]
})
export class UpdateComponent {
  @Input() receivedRow:DataTable;
   public dt: NgbDateStruct;
   public dt2: NgbDateStruct;
   public startCheck: boolean = false;
   public endCheck: boolean = false;
   updateForm : FormGroup;

   constructor(fb: FormBuilder, private datePipe: DatePipe, private appref: ApplicationRef){
     this.updateForm = fb.group({
      'dataPoint' : [null, Validators.required],
      'ICCP' : [null, Validators.required],
      'startDate' : [null, Validators.required],
      'endDate' : [null, Validators.required]
      }, {validator: this.endDateAfterOrEqualValidator})
   }

 ngOnChanges(){
if(this.receivedRow){
  this.updateForm.controls['dataPoint'].setValue(this.receivedRow.tDataPoint);
  this.updateForm.controls['ICCP'].setValue(this.receivedRow.tICCP);
  this.updateForm.controls['startDate'].setValue(this.receivedRow.tStartDate);
  this.updateForm.controls['endDate'].setValue(this.receivedRow.tEndDate);
  }
}

  resetForm(){
    location.reload();
    //this.updateForm.reset();
  }

  submitForm(value: any, originalRow: any){
    var dataPointID = originalRow.receivedRow.tDataPoint;
    for (let entry in DPS) {
      if (DPS[entry].tDataPoint === dataPointID) {
        DPS[entry].tDataPoint = String((<HTMLInputElement>document.getElementById("dataPoint")).value);
        DPS[entry].tICCP = String((<HTMLInputElement>document.getElementById("ICCP")).value);
        DPS[entry].tStartDate = String((<HTMLInputElement>document.getElementById("startDate")).value);
        DPS[entry].tEndDate = String((<HTMLInputElement>document.getElementById("endDate")).value);
      }
    }
  }

  getStartDate(){
    var month = this.receivedRow.tStartDate.substring(0,2);
    var day = this.receivedRow.tStartDate.substring(3,5);
    var year = this.receivedRow.tStartDate.substring(6,10);
    var dateToUse = new Date(Number(year),Number(month)-1,Number(day));
    let timestamp = this['startDate'] != null ? new Date(this['startDate'].year, this['startDate'].month-1, this['startDate'].day).getTime() : dateToUse.getTime();
    this.updateForm.controls['startDate'].setValue(this.datePipe.transform(timestamp, 'MM/dd/yyyy'));
    this.appref.tick();
  }

  getEndDate(){
    var month = this.receivedRow.tEndDate.substring(0,2);
    var day = this.receivedRow.tEndDate.substring(3,5);
    var year = this.receivedRow.tEndDate.substring(6,10);
    var dateToUse = new Date(Number(year),Number(month)-1,Number(day));
    let timestamp = this['endDate'] != null ? new Date(this['endDate'].year, this['endDate'].month-1, this['endDate'].day).getTime() : dateToUse.getTime();
    this.updateForm.controls['endDate'].setValue(this.datePipe.transform(timestamp, 'MM/dd/yyyy'));
    this.appref.tick();
  }

  public showDatePick(selector):void {
     if(selector === 0) {
       this.startCheck = !this.startCheck
     } else {
       this.endCheck = !this.endCheck;
     }
  }

  endDateAfterOrEqualValidator(formGroup): any {
    var startDateTimestamp, endDateTimestamp;
    for(var controlName in formGroup.controls) {
      if (controlName.indexOf("startDate") !== -1) {
       startDateTimestamp = Date.parse(formGroup.controls[controlName].value);
      }
      if (controlName.indexOf("endDate") !== -1) {
        endDateTimestamp = Date.parse(formGroup.controls[controlName].value);
      }
    }
    return (endDateTimestamp < startDateTimestamp) ? { endDateLessThanStartDate: true } : null;
  }
}
import { Component, Input, ChangeDetectorRef } from '@angular/core';

Inject 注入

constructor(private cdRef:ChangeDetectorRef) {}

and use it 并使用它

public showDatePick(selector):void {
  if(selector === 0) {
    this.startCheck = !this.startCheck
  } else {
    this.endCheck = !this.endCheck;
  }
  this.cdRef.detectChanges();
}

Have you tried using ChangeDetectorRef ? 您是否尝试过使用ChangeDetectorRef

constructor(private changeDetector: ChangeDetectorRef) {
}

And detect the changes with 并用它来检测变化

changeDetector.detectChanges();

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

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