javascript - 如何调用函数,如果 bool 值在 Angular2 中为真?

标签 javascript angular typescript

我在这里写了一个弹出函数,一个 bool 标志是它们在我的组件类中。我的标志将根据我的条件返回 true 或 false。在模板类中,弹出函数需要在标志变为真时触发,然后我的弹出对话框会立即出现。但我不知道调用正确的方法,如果有人知道请帮助我正确的方法。

<ng-template #sessionSuccessModal let-c="close" let-d="dismiss">
	<div class="modal-header">
			<h4 class="modal-title">Include Criteria Error</h4>
			<button type="button" class="close" aria-label="Close" (click)="closeModel()">
			<span aria-hidden="true">&times;</span>
			</button>
	</div>
	<div class="modal-body"  class="bg-light text-dark">
			<p>{{ alertMessage }}!</p>
	</div>
	<div style="text-align: center" class="bg-light text-dark">
			<button type="button"  (click)="closeModel()">Ok</button>
	
	</div>
</ng-template>

最初 commaSeparation 将为 false,this.commaSeparation = this.genericValidator.validateMultiComma(this.orderUnitForm);此函数返回 true 或 false。如果是这样,那么我需要调用我的 displayModel() 警报方法。现在弹出窗口工作正常,调用 ngAfterViewInit() 但在控制台中出现错误。

ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。先前的值:'ng-untouched: true'。当前值:'ng-untouched: false'

 ngAfterViewInit() {
        let controlBlurs: Observable<any>[] = this.formControls
            .map((formControl: ElementRef) => Observable.fromEvent(formControl.nativeElement, 'blur'));
// debounceTime(1000)/
        Observable.merge(this.orderUnitForm.valueChanges, ...controlBlurs).subscribe(value => {
            this.displayMessage = this.genericValidator.processMessages(this.orderUnitForm);
           // this.valid = this.genericValidator.validateControls(this.orderUnitForm);
        });
        this.orderUnitForm.valueChanges.debounceTime(1000).subscribe(value => {
            this.valid = this.genericValidator.validateControls(this.orderUnitForm);
            this.commaSeparation = this.genericValidator.validateMultiComma(this.orderUnitForm);
              if(this.commaSeparation == true){
                this.displayModel();
              }
        });
  }

这是我的 dispalyModel() 函数:

displayModel() {
     this.alertMessage = 'You cannot enter more than one multiple at the same time ';
     this.successErrorModalBlock = this.modalService.open(this.sessionSuccessModalref);
   }

最佳答案

您可以通过实现作为 Angular 生命周期 Hook 的接口(interface) OnChanges 来实现这一点。

import { Component, OnChanges, SimpleChanges } from '@angular/core';


@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnChanges  {

  // the flag property
  commaSeparation: boolean;

  ngOnChanges(changes: SimpleChanges){
    if (changes['commaSeparation'] && changes['commaSeparation'].currentValue){
        this.showPopup();
    }
  }

  public showPopup(){
        alert('Replace this alert by the code that shows your popup');
  }
}

引用:https://angular.io/guide/lifecycle-hooks#onchanges

关于javascript - 如何调用函数,如果 bool 值在 Angular2 中为真?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51689898/

相关文章:

typescript - 什么? : mean in TypeScript?

angular - 为什么 Typescript 无法推断 RxJs 流中的元组返回类型?

javascript - 为什么我需要将代码包装在立即调用的函数表达式中?

javascript - 正则表达式在指令中不起作用 - Angular 4

javascript - 如何将 chart.js 图表保存在一个 JS 文件中,并且当 JS 文件中的 ID 在一个特定的 html 页面上不存在时不会出现错误?

php - 当我从硬编码的下拉列表中选择值时,它不会选择值并且不会将其发送到数据库中

Angular2 rxjs 按可观察字段对对象列表进行排序(可观察)

forms - Angular 2 : disable button if there are changes in form

javascript - 如何停止在 DOMSubtreeModified 事件上冒泡?

Oracle_ADF 12.2.1 中的 JavaScript 图像 slider