简体   繁体   English

如何在Angular 4中推送到Observable of Array? RxJS

[英]How to push to Observable of Array in Angular 4? RxJS

I have a property on my service class as so: 我的服务类上有一个属性,如下所示:

articles: Observable<Article[]>;

It is populated by a getArticles() function using the standard http.get().map() solution. 它由getArticles()函数填充,使用标准的http.get().map()解决方案。

How can I manually push a new article in to this array; 如何手动将新文章推送到此数组中; One that is not yet persisted and so not part of the http get? 一个还没有坚持,所以不是http的一部分?

My scenario is, you create a new Article, and before it is saved I would like the Article[] array to have this new one pushed to it so it shows up in my list of articles. 我的方案是,你创建一个新文章,在保存之前我希望Article []数组将这个新文章推送到它,以便它显示在我的文章列表中。

Further more, This service is shared between 2 components, If component A consumes the service using ng OnInit() and binds the result to a repeating section *ngFor , will updating the service array from component B simultaneously update the results in components A's ngFor section? 此外,此服务在两个组件之间共享,如果组件A使用ng OnInit()消耗服务并将结果绑定到重复节*ngFor则将从组件B更新服务数组,同时更新组件A的ngFor节中的结果? Or must I update the view manually? 或者我必须手动更新视图吗?

Many Thanks, Simon 非常感谢,西蒙

As you said in comments, I'd use a Subject. 正如你在评论中所说,我会使用一个主题。

The advantage of keeping articles observable rather than storing as an array is that http takes time, so you can subscribe and wait for results. 保持articles可观察而不是存储为数组的优点是http需要时间,因此您可以订阅并等待结果。 Plus both components get any updates. 此外,两个组件都可以获得任

 // Mock http const http = { get: (url) => Rx.Observable.of(['article1', 'article2']) } const articles = new Rx.Subject(); const fetch = () => { return http.get('myUrl').map(x => x).do(data => articles.next(data)) } const add = (article) => { articles.take(1).subscribe(current => { current.push(article); articles.next(current); }) } // Subscribe to articles.subscribe(console.log) // Action fetch().subscribe( add('article3') ) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.2/Rx.js"></script> 

Instead of storing the whole observable, you probably want to just store the article array, like 您可能只想存储文章数组,而不是存储整个observable

articles: Article[]

fetch() {
    this.get(url).map(...).subscribe(articles => this.articles)
}

Then you can manipulate the articles list using standard array manipulation methods. 然后,您可以使用标准数组操作方法操作articles列表。

If you store the observable, it will re-run the http call every time you subscribe to it (or render it using | async ) which is definitely not what you want. 如果你存储了observable,它会在你每次订阅时重新运行http调用(或使用| async渲染它),这绝对不是你想要的。

But for the sake of completeness: if you do have an Observable of an array you want to add items to, you could use the map operator on it to add a specified item to it, eg 但是为了完整起见:如果你想要添加项目的数组的Observable,你可以使用它上面的map运算符向它添加一个指定的项目,例如

observable.map(previousArray => previousArray.concat(itemtToBeAdded))

ex from angular 4 book ng-book 来自有角度的4本书

Subject<Array<String>> example =  new Subject<Array<String>>();


push(newvalue:String):void
{
  example.next((currentarray:String[]) : String[] => {
    return  currentarray.concat(newValue);
   })

}

what the following says in example.next is take the current array value Stored in the observable and concat a new value onto it and emit the new array value to subscribers. 下面在example.next中说的是将当前数组值存储在observable中并在其上连接一个新值并向订阅者发出新的数组值。 It is a lambda expression.I think this only works with subject observables because they hold unto the last value stored in their method subject.getValue(); 它是一个lambda表达式。我认为这只适用于subject observables,因为它们保存在方法subject.getValue()中存储的最后一个值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM