我试图了解如何在Angular 2中使用Observables.我有这个服务:
import {Injectable, EventEmitter, ViewChild} from '@angular/core';
import {Observable} from "rxjs/Observable";
import {Subject} from "rxjs/Subject";
import {BehaviorSubject} from "rxjs/Rx";
import {Availabilities} from './availabilities-interface'
@Injectable()
export class AppointmentChoiceStore {
public _appointmentChoices: BehaviorSubject<Availabilities> = new BehaviorSubject<Availabilities>({"availabilities": [''], "length": 0})
constructor() {}
getAppointments() {
return this.asObservable(this._appointmentChoices)
}
asObservable(subject: Subject<any>) {
return new Observable(fn => subject.subscribe(fn));
}
}
Run Code Online (Sandbox Code Playgroud)
此BehaviorSubject从另一个服务推送新值:
that._appointmentChoiceStore._appointmentChoices.next(parseObject)
Run Code Online (Sandbox Code Playgroud)
我在组件中以可观察的形式订阅它,我希望将其显示在:
import {Component, OnInit, AfterViewInit} from '@angular/core'
import {AppointmentChoiceStore} from '../shared/appointment-choice-service'
import {Observable} from 'rxjs/Observable'
import {Subject} from 'rxjs/Subject'
import {BehaviorSubject} from "rxjs/Rx"; …Run Code Online (Sandbox Code Playgroud) 检查用户是否在没有jQuery的情况下滚动到Angular2页面底部的最佳做法是什么?我是否可以访问我的应用程序组件中的窗口?如果不是我应该检查滚动到页脚组件的底部,我该怎么做?关于页脚组件的指令?有没有人完成这个?
我正在编写我的第一个完整程序,并进行了两周的编程,但遇到了一个我似乎无法弄清楚的障碍。我正在制作一个连接 4 游戏,并且在推送到 DOM 之前通过在 JavaScript 中构建逻辑开始。我已经开始使用由构造函数创建的单元格对象来制作它,然后以 2D 数组的形式将这些单元格对象推送到游戏对象中。我设法创建了一个每次都进行播放的函数,并使用 2 天数组更改该列最低点的单元格值。但是,我不确定如何让我检查 wins 功能运行。
到目前为止,我的逻辑是,对于 2D 数组中的每个点,您可以按行、按列和按对角线检查。我了解如何检查获胜的逻辑,但我不明白如何按行和列遍历数组。在下面的示例中, this.cellsArray 是 Board Constructor 中的单元格对象数组。该阵列有 7 个列阵列,每个阵列有 6 行,因为我翻转了典型的行列逻辑以考虑 Connect Four 基于列的性质。但是我不能像 this.cellsArray[col][row] 那样访问数组,因为 col 和 row 没有定义,我不确定如何定义索引值?任何帮助,将不胜感激!
例子:
//array location is equal to an instance of this.cellsArray[col][row]
Board.prototype.checkRowRight = function (arrayLocation) {
if ((arrayLocation[i+1][i].value === arrayLocation.value) && (arrayLocation[i+2][i]=== arrayLocation.value) && (arrayLocation[i+3][i].value === arraylocation.value)){
this.winner = this.currentPlayer;
this.winnerFound = true;
console.log('Winner has been found!')
}
};
Run Code Online (Sandbox Code Playgroud) 寻找Observable http序列的帮助,我想对api进行两次http调用,第二次依赖于第一次.第一个返回一个Urls数组,第二个为该数组中的每个url生成get请求,然后返回流上的响应.如果我在其中一个依赖请求中硬编码,那么我会找回我正在寻找的一个标题:
search(character): Observable<any> {
let that = this
let queryUrl: string = character.url;
return this.http.get(queryUrl)
.map((response: Response) => {
this.characterResults = response.json().films
return this.characterResults
//Example response:
// ["https://api.com/films/1", "https://api.com/films/2", "https://api.com/films/3", "https://api.com/films/4"]
})
.flatMap((film) => {
return that.http.get(film[0])
.map((response: Response) => {
return response.json().title
})
})
}
getApiData(character) {
this.apiService.search(character)
.subscribe((results) => { // on sucesss
console.log(results)
},
(err: any) => { // on error
console.log(err);
},
() => { // on completion
console.log('complete')
}
);
Run Code Online (Sandbox Code Playgroud)
但任何尝试使用forEach迭代该数组然后进行所有http调用都会给我这个错误:
browser_adapter.js:84 EXCEPTION: TypeError: …Run Code Online (Sandbox Code Playgroud)