小编doe*_*doe的帖子

如何在不触发表单更改事件的情况下将 FormGroup 添加到 FormArray

我用任何空数组初始化表单,definitionProperties如何更新FormGroup?我需要在不触发更改事件的情况下执行此操作,否则会导致valueChanges可观察对象中的无限循环。

this.gridProcessorForm = this.fb.group({
    propertiesSide: new FormControl(),
    objectNamesSide: new FormControl(),
    definitionProperties: this.fb.array([])
});
Run Code Online (Sandbox Code Playgroud)

这会导致无限循环:

this.gridProcessorForm.valueChanges.subscribe(val => {
    let propertyGroups: FormGroup[] = [];
    for (let property of this.objectDefinition.properties) {
      let group = this.createPropertyFormGroup(property);
      (this.gridProcessorForm.get('definitionProperties') as FormArray).push(group); // triggers change event so infinite loop
    }
});
Run Code Online (Sandbox Code Playgroud)

当使用 setValue with{ emitEvent: false }覆盖 FormArray 时,它会出错。

this.gridProcessorForm.valueChanges.subscribe(val => {
  if (this.gridSideSelection = 'top') {        
    let propertyGroups: FormGroup[] = [];
    for (let property of this.objectDefinition.properties) {
      let …
Run Code Online (Sandbox Code Playgroud)

angular

5
推荐指数
1
解决办法
1900
查看次数

获取 Angular 组件的滚动位置

如何使用带有 fromEvent 的组件获取滚动位置?

我用它来获取鼠标位置,如何从顶部位置滚动?

this.mouseMoveSubscription = fromEvent(this.elementRef.nativeElement, 'mousemove')
                  .subscribe((e: MouseEvent) => {...});
Run Code Online (Sandbox Code Playgroud)

这不起作用:

ngOnInit() {
    this.scrollSubscription = fromEvent(this.elementRef.nativeElement, 'scroll')
                  .subscribe((e: Scroll) => {
                    console.log(e.position);
                  });

  }
Run Code Online (Sandbox Code Playgroud)

也没有为此触发任何事件(来自:answer):

@HostListener('scroll', ['$event']) // for scroll events of the current element
onScroll(event) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

angular

3
推荐指数
1
解决办法
1万
查看次数

ngFor Observable 与 ngIf 中的异步管道不显示任何结果

我有一个带有下拉列表的输入,但不显示结果。它在带有异步的 ngIf 中。当我删除 ngIf 代码时会显示下拉列表,因此我认为它必须是 ngIf 检查或我如何设置测试可观察服务。当我按下一个键输入 1 个值时,不会显示任何内容,第二次输入一个键时,它会显示列表。

<div class="format-options" *ngIf="(definitions | async)?.length > 0">
  <div *ngFor="let definition of definitions | async" class="search-result">
    <div>{{definition.name}}</div>
    <div>{{definition.description}}</div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

搜寻服务:

searchTerm(term: string): Observable<Array<ObjectDefinition>>

    let observer = new Observable<Array<ObjectDefinition>>((subscription) => {

      const timer$ = interval(1000);

      timer$.subscribe(() => {
        console.log('timer 1 second');
        subscription.next(this.objectDefinitions);
        subscription.complete();
      });

    });

    return observer;
}
Run Code Online (Sandbox Code Playgroud)

成分:

constructor(private definitionService: DefinitionService) {
    this.definitions = this.searchInput.valueChanges
                        .pipe(
                            tap(value => console.log('input')),
                            //startWith(''),
                            debounceTime(500),
                            //distinctUntilChanged(),
                            switchMap(value => this.definitionService.searchTerm(value))
                        );
  }
Run Code Online (Sandbox Code Playgroud)

rxjs angular

2
推荐指数
1
解决办法
1722
查看次数

如何在不触发新发射的情况下获取可观察值

我如何获得可观察值的发射值而又不触发新发射?我想在ngClass标记中获取可观察值。

我尝试使用管道水龙头来获取通过switchMap传递的值,但没有记录该值。然后,我可以使用trueIfDefinitionsLengthngClass条件。

this.definitions$.pipe(
  tap(value => console.log('timer result =', value))
)
Run Code Online (Sandbox Code Playgroud)

观察者订阅| 异步的

模板:

<input [ngClass]="{'input-has-results': trueIfDefinitionsLength > 0}"
       [formControl]="searchInput">

<ng-container *ngIf="definitions$ | async as definitions">
  <div class="format-options" *ngIf="definitions.length > 0">
    <div *ngFor="let definition of definitions" class="search-result">
      <div>{{definition.name}}</div>
      <div>{{definition.description}}</div>
    </div>
  </div>
</ng-container>
Run Code Online (Sandbox Code Playgroud)

零件

this.definitions$ = this.searchInput.valueChanges
                        .pipe(
                            tap(value => console.log('input')),
                            //startWith(''),
                            debounceTime(500),
                            //distinctUntilChanged(),
                            switchMap(value => this.definitionService.searchTerm(value))
                        );
Run Code Online (Sandbox Code Playgroud)

rxjs typescript angular

1
推荐指数
1
解决办法
53
查看次数

标签 统计

angular ×4

rxjs ×2

typescript ×1