答案 0 :(得分:6)
您订阅了两次观察。订阅后异步管道在内部执行。
当执行subscribe
方法时,它会执行subscribe
函数
observer => this.observer = observer
并覆盖this.observer
属性,因此它只对异步管道(最后一个用户)有效
我会使用share
运算符来解决它
new Observable(observer => this.observer = observer).share();
<强> Plunker Example 强>
要查看为什么this.observer
被覆盖,请运行此代码
let myObserver;
const observable$ = new Rx.Observable(function subscribe(observer) {
console.log('subscribe function has been called');
myObserver = observer;
});
observable$.subscribe(function next1() { console.log('next1'); });
observable$.subscribe(function next2() { console.log('next2'); });
observable$.subscribe(function next3() { console.log('next3'); });
myObserver.next();
正如我所提到的,早期异步管道订阅内部可观察
https://github.com/angular/angular/blob/4.3.x/packages/common/src/pipes/async_pipe.ts#L23
答案 1 :(得分:1)
您应该使用
[ngValue]="value"
而不是[value]
将值指定为
public selectedValue: string = 'test';
HTML应更改为
<select [(ngModel)]="selectedValue">
<option *ngFor="let value of values$ | async"
[ngValue]="value">{{ value }}
</option>
</select>
<强> Updated Plunker 强>
答案 2 :(得分:1)
您应该将其绑定到ngValue:
<div>
<h2>Hello {{name}}</h2>
</div>
<select [(ngModel)]="selectedValue">
<option *ngFor="let value of (values$ | async)"
[ngValue]="selectedValue">{{ value }}
</option>
</select>