有一个经过调整和工作的 plunker
我会用一个EventEmitter
和几个运营商来实现它,主要是
调整后的代码
data = ["one","two","three"]
data$: Observable<string[]>;
protected emitter = new EventEmitter<string[]>();
constructor() {
this.name = 'Angular2 array to observable example'
this.data$ = this.emitter
.startWith(this.data)
.scan((orig, item) => orig.concat(item))
}
ngOnInit() {
// this.data$ = Rx.Observable.of(this.data)
// .map(data => {
// let x = data
// x.push("4")
// return x
// })
}
addToArray() {
//this.data.push('more numbers')
this.emitter.emit("forth")
}
在这里检查
延长
更复杂的plunker
还有更复杂的解决方案.. 只是从 Observable 及其 Operators 中获利。它已准备好添加和删除项目:
data = ["one","two","three"]
data$: Observable<string[]>;
protected emitter = new EventEmitter<string[]>();
protected toDelete = new Rx.BehaviorSubject<string[]>([])
.scan((orig, item) => orig.concat(item));
constructor() {
this.name = 'Angular2 array to observable example'
this.data$ = this.emitter
// start
.startWith(this.data)
// return array
.scan((orig, item) => orig.concat(item))
// adjust each source string with a prefix
.map((coll: string[]) => {
let adjusted: string[] = []
coll.forEach(item => {
adjusted.push("x" + item)
})
return adjusted;
})
// now consume also array of items to be deleted
.combineLatest(this.toDelete)
// just those which were not delted
.map(([all, toDelete]:[string[], string[]]) =>{
let result = all.filter( function( el ) {
return toDelete.indexOf( el ) < 0;
});
return result;
})
}
counter: int = 0;
addToArray() {
this.emitter.emit(`other${++this.counter}`)
}
deleteFromArray(removeString) {
this.toDelete.next(removeString)
}
在此处查看实际情况
让我们再做一次 EXTEND
有一个最终的 plunker 有很多data: string\[\]
数组处理
我们现在甚至可以跟踪更改并让他们调整原始数据数组,甚至可以使用 RESET 功能重新开始。这是调整后的代码:
data = ["one","two","three"]
data$: Observable<string[]>;
protected emitter: EventEmitter<string[]>;
protected toDelete: Rx.BehaviorSubject<string[]>;
constructor() {
this.initEmitters();
this.data$ = this.createObservable(this.data);
}
initEmitters() {
this.emitter = new EventEmitter<string[]>();
this.toDelete = new Rx.BehaviorSubject<string[]>([])
.scan((orig, item) => orig.concat(item));
}
createObservable(initData)
{
let observable = this.emitter
// start
.startWith(initData)
// return array
.scan((orig, item) => orig.concat(item))
// adjust each source string with a prefix
.map((coll: string[]) => {
let adjusted: string[] = []
coll.forEach(item => {
adjusted.push("x" + item)
})
return adjusted;
})
// now consume also array of items to be deleted
.combineLatest(this.toDelete)
// just those which were not delted
.map(([all, toDelete]:[string[], string[]]) =>{
let result = all.filter( function( el ) {
return toDelete.indexOf( el ) < 0;
});
return result;
})
observable
.subscribe((currentData) => {
this.data.length = 0;
[].push.apply(this.data, currentData)
});
return observable;
}
counter: int = 0;
addToArray() {
this.emitter.emit(`other${++this.counter}`)
}
deleteFromArray(removeString) {
this.toDelete.next(removeString)
}
resetArray() {
this.initEmitters();
this.data$ = this.createObservable(['ten','eleven'])
}
在此处测试array
与obesrvable
实际操作