简体   繁体   English

如何从服务访问属性类型BehaviorSubject?

[英]How can I take access to property type BehaviorSubject from service?

I have a TttService service. 我有一个TttService服务。 It has a cells property. 它具有单元格属性。 This is an array of objects. 这是一个对象数组。 It changes only on the client. 它仅在客户端上更改。 One component subscribes to its changes, the other component changes it. 一个组件订阅其更改,另一组件更改它。 I want to make makeMove() change one of the properties of the desired object in the cell array and that the component that is signed on the cell should get these changes. 我想使makeMove()更改单元格数组中所需对象的属性之一,并且在单元格上签名的组件应获得这些更改。 How can this be realized? 如何实现呢?

If in method makeAMove() to make console.log(this.cells) - there will be empty. 如果在方法makeAMove()中创建console.log(this.cells)-将为空。 In this case, the component that was subscribed to cells through the initCells() method gets modified for the first time. 在这种情况下,通过initCells()方法订阅单元的组件是第一次被修改。

My service 我的服务

 import { Injectable } from '@angular/core'; import { TttServiceInteface } from '../interfaces/ttt-service-inteface'; import { CellInteface } from '../interfaces/cell-interface'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/of'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { asObservable } from './asObservable'; @Injectable() export class TttService { public cells: BehaviorSubject<CellInteface[]> = new BehaviorSubject([]); constructor() { } initCells(fieldSize: number, fieldWidth: number): Observable<CellInteface[]> { const cells = []; for (let i = 0; i < fieldSize * fieldSize; i++) { cells.push({ width: fieldWidth, height: fieldWidth, id: i }); } this.cells.next(cells); return this.cells; } getCells() { return asObservable(this.cells); } makeAMove(id: number) { this.getCells() .subscribe((c: Array<CellInteface>) => { c.forEach(cell => { if (cell.id === id && !cell.id) { cell.value = 1; } }); this.cells.next(c); }); } 

My component 我的组件

 ngOnInit() { const border = (window.screen.availHeight - 150); this.fieldSize = 5; this.border = border + 100; this.tttService .initCells(this.fieldSize, border / this.fieldSize) .subscribe(cells => { console.log(cells); this.cells = cells; }); } 

Try this. 尝试这个。

In your service. 为您服务。

 private cells: BehaviorSubject<CellInteface[]> = new BehaviorSubject([]);
 private arrayOfCells: CellInterface[];

 public initCells(fieldSize: number, fieldWidth: number): void {

    for (let i = 0; i < fieldSize * fieldSize; i++) {
      this.arrayOfCells.push({
        width: fieldWidth,
        height: fieldWidth,
        id: i
      });
    }
    this.cells.next(this.arrayOfCells);
  }

 public getCells(): Observable<CellInteface[]> {
    return this.cells.asObservable();
  }

  public makeAMove(id: number) {
    this.arrayOfCells.forEach(cell => {
      if (cell.id === id && !cell.id) {
        cell.value = 1;
      }
    });
    this.cells.next(this.arrayOfCells);
  }

In the subscribing component. 在订阅组件中。

constructor(private tttService: YourService ) {
   this.tttService.getCells().subscribe((cells: CellInteface[]) => {
      // you can use cells variable in here. 
    });
}

In the data setting component. 在数据设置组件中。

constructor(private tttService: YourService ) {
       const border = (window.screen.availHeight - 150);
       this.fieldSize = 5;
       this.border = border + 100;

       this.tttService.initCells(this.fieldSize, border / this.fieldSize);

  //or you can call the makeAMove method.
       this.tttService.makeAMove(id).subscribe((res) => {
          // you get the updated cell array as res
       });
  }

暂无
暂无

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

相关问题 如何从服务中的其他服务订阅BehaviorSubject? - How can I subscribe to a BehaviorSubject from another service in a service? 如何spyOn属性为BehaviorSubject / Observable的服务? - How to spyOn property of a Service that is a BehaviorSubject/Observable? 如何将数字分配给“行为主题”类型<number> '</number> - How can I assign a number to a type 'BehaviorSubject<number>' 如何从 HTTP 获取此服务中的数据? - How can I take the data in this service from an HTTP get call? 如何将BehaviorSubject的属性值绑定到ng-template的let- *? - How can I bind BehaviorSubject's property value to let-* of ng-template? 我如何在不从我的服务传递属性“类型”的情况下使用带有元数据的效果? - How i can use effects with metadata without passing the property “type” from my service? 从 Angular 的组件中按索引访问 BehaviorSubject 上的索引类型? - Access an indexed type by index on a BehaviorSubject from a component in Angular? 如何将 BehaviorSubject 传递给 MatDialog 数据? - How can i pass BehaviorSubject to MatDialog data? 我应该如何在Angular 4中的组件的HTML模板中使用Service中的BehaviorSubject类? - How should I use BehaviorSubject class from a Service in the HTML Template of a Component in Angular 4? "Angular 获取 BehaviorSubject 值错误:“从不”类型上不存在属性“access_token”" - Angular getting BehaviorSubject value error: Property 'access_token' does not exist on type 'never'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM