有谁为什么这个代码(从主题初始化一个值)不起作用?有没有错误或设计?我究竟做错了什么?
TS
import { Component, OnInit } from '@angular/core';
import { Subject } from "rxjs";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.styl']
})
export class AppComponent implements OnInit {
itemSupplier$: Subject<any[]> = new Subject<any[]>();
items: any[] = [
{name: 'Item 1', value: 'item1'},
{name: 'Item 2', value: 'item2'},
];
ngOnInit(){
this.itemSupplier$.next(this.items);
}
}
Run Code Online (Sandbox Code Playgroud)
HTML
<ul>
<li *ngFor="let item of itemSupplier$ | async">{{item.name}}</li>
</ul>
Run Code Online (Sandbox Code Playgroud) 我正在从块中接收来自firebase服务器的数据,同时渲染数据需要一个坚持observable包含Array的库.我在某种程度上无法将新数据块推送到observable包含的现有数据块数组,
从dataservice我按照主题的下一个调用并尝试添加一个新的calEvent
this.homeWorkerService.eventSubject.next(calEvent);
Run Code Online (Sandbox Code Playgroud)
在组件中,我有以下代码
events$: Observable<Array<CalendarEvent<any>>>;
Run Code Online (Sandbox Code Playgroud)
和ngOnInit,我正在向它提供数据
this.events$ = this.service.eventSubject.asObservable();
Run Code Online (Sandbox Code Playgroud)
你能否建议我可以通过哪种方式向已经举行活动的观察者添加新活动.
PS:我使用这个 lib来渲染日历并使用remoteDB来渲染事件.
谢谢,
我正在使用subject作为我的angular2应用程序中组件的通信服务的服务.
通话方式:
this.notification.create('test');
Run Code Online (Sandbox Code Playgroud)
服务:
export class NotificationService {
private static notificationSource = new Subject<any>()
notiCreated$ = NotificationService.notificationSource.asObservable();
create(message) {
NotificationService.notificationSource.next(message);
}
}
Run Code Online (Sandbox Code Playgroud)
被叫功能:
this.subscription = notification.notiCreated$.subscribe(
data => {
console.log(data);
this.createNotification(data, 'warning');
});
Run Code Online (Sandbox Code Playgroud)
但我想通过2个参数.我有错误.
我该怎么做?
虽然我知道如何计算R中的平均值,但我似乎无法理解如何在不同条件下进行计算.我读过的其他帖子并不复杂.
> Target/dictractor TrialType Bin0 Bin1 Bin2 Bin3
1 Target 2C 3 0 2 0
1 Target 2C 2 0 3 0
1 Target 2E 0 1 1 2
1 Target 2E 0 0 0 0
1 Distractor 2C 0 3 0 1
1 Distractor 2C 0 0 0 0
1 Distractor 2E 0 0 1 0
1 Distractor 2E 0 0 0 3
2 Target 2C 1 1 0 1
2 Target 2C 2 0 0 2
2 Distractor …Run Code Online (Sandbox Code Playgroud) 在我的例子中有三个证书,假设它们形成一个链但我不知道它们中哪个签署了哪个:
X509Certificate c1 = ....
X509Certificate c2 = ....
X509Certificate c2 = ....
Run Code Online (Sandbox Code Playgroud)
我想知道哪个证书负责签署另一个证书.
计划是获取" AuthorityKeyIdentifier "并将其与" SubjectKeyIdentifier " 匹配.
import org.bouncycastle.asn1. DEROctetString;
private static String decodeKey(byte[] e) {
DEROctetString octet = new DEROctetString(e);
return octet.toString();
}
String subjectKeyId = decodeKey(c.getExtensionValue("2.5.29.14"));
String authorityKeyId = decodeKey(c.getExtensionValue("2.5.29.35"));
Run Code Online (Sandbox Code Playgroud)
我得到以下证书(按照链的顺序):主题/权限密钥ID对
解码后SubjectKeyIdentifier和AuthorityKeyIdentifier的值:
证书1 :(链的末尾)
#0416041482b7384a93aa9b10ef80bbd954e2f10ffb809cde
#04183016801482b7384a93aa9b10ef80bbd954e2f10ffb809cde
Run Code Online (Sandbox Code Playgroud)
证书2:由证书1签署
#04160414ab8059c365836d1d7d13bd19c3ec1a8f0d476aa3
#04183016801482b7384a93aa9b10ef80bbd954e2f10ffb809cde
Run Code Online (Sandbox Code Playgroud)
证书3:由证书2签署
(no SubjectKeyIdentifier - null bytes)
#041830168014ab8059c365836d1d7d13bd19c3ec1a8f0d476aa3
Run Code Online (Sandbox Code Playgroud)
格式化和对齐以便于阅读(与顶部相同)
------------------------------------------------------------------------------
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 …Run Code Online (Sandbox Code Playgroud) 我想在Angular2服务中编写一个简单的切换.
因此,我需要Subject观察的当前值(见下文).
import {Injectable} from 'angular2/core';
import {Subject} from 'rxjs/Subject';
@Injectable()
export class SettingsService {
private _panelOpened = new Subject<boolean>();
panelOpened$ = this._panelOpened.asObservable();
togglePanel() {
this._panelOpened.next(!this.panelOpened$);
}
}
Run Code Online (Sandbox Code Playgroud)
如何从_panelOpened/panelOpened $中获取当前值?
谢谢.
我正在尝试为角度服务编写测试,该服务具有Subject属性和调用.next()该主题的方法.
该服务如下:
@Injectable()
export class SubjectService {
serviceSubjectProperty$: Subject<any> = new Subject();
callNextOnSubject(data: any) {
this.serviceSubjectProperty$.next(data);
}
}
Run Code Online (Sandbox Code Playgroud)
以及该服务的测试文件:
import { TestBed, inject } from '@angular/core/testing';
import { SubjectService } from './subject.service';
describe('SubjectService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
SubjectService
]
});
});
it('callNextOnSubject() should emit data to serviceSubjectProperty$ Subject',
inject([SubjectService], (subjectService) => {
subjectService.callNextOnSubject('test');
subjectServiceProperty$.subscribe((message) => {
expect(message).toBe('test');
})
}));
});
Run Code Online (Sandbox Code Playgroud)
如果我将subjectService.callNextOnSubjectfrom 的参数更改'test'为其他任何内容,则测试始终会传递事件.
我也尝试用async和包装所有东西fakeAsync,但结果是一样的.
测试是否callNextOnSubject …
我的AlertService有
private subject = new Subject<Alert>();。我想在5秒钟后专心清除警报。我可以这样使用setTimeout():
autoClear(alertId?: string) {
setTimeout(
() => this.subject.next(new Alert({alertId})),
5000);
}
Run Code Online (Sandbox Code Playgroud)
我尝试使它更优雅,并创建了以下代码:
autoClear(alertId?: string) {
const delay = new Observable(x => {
x.next();
}).delay(5000).subscribe(() => {
this.subject.next(new Alert({alertId}));
delay.unsubscribe();
});
}
Run Code Online (Sandbox Code Playgroud)
这两个示例都可以工作,但看起来不像是使用RxJS的正确方法。我该如何改善?
我有一个Observable产生单播值(对于所有观察者来说都是如此)。但是,当我打算使用RxJs多播运算符转换为多播时,它将返回以下错误。
属性“连接”在类型“可观察”上不存在
单播(工作代码)-
let source4$ = interval(1000).pipe(take(4));
source4$.subscribe(val => {
console.log(`Observer 1: ${val}`);
});
setTimeout(function() {
source4$.subscribe(val => {
console.log(`Observer 2: ${val}`);
});
}, 1000);
setTimeout(function() {
source4$.subscribe(val => {
console.log(`Observer 3: ${val}`);
});
}, 2000);
Run Code Online (Sandbox Code Playgroud)
组播(无效代码)-
let source4$ = interval(1000).pipe(take(4), multicast(new Subject()));
source4$.subscribe(val => {
console.log(`Observer 1: ${val}`);
});
setTimeout(function() {
source4$.subscribe(val => {
console.log(`Observer 2: ${val}`);
});
}, 1000);
setTimeout(function() {
source4$.subscribe(val => {
console.log(`Observer 3: ${val}`);
});
}, 2000);
source4$.connect();
Run Code Online (Sandbox Code Playgroud) 我发现自己对尝试设置一个非常简单的 rxjs 订阅流程感到困惑。将多个不相关的订阅嵌套到另一个订阅中。
我在一个有角度的应用程序中,在进行其他订阅之前我需要填写一个主题。
这将是我想要实现的嵌套版本。
subject0.subscribe(a => {
this.a = a;
subject1.subscribe(x => {
// Do some stuff that require this.a to exists
});
subject2.subscribe(y => {
// Do some stuff that require this.a to exists
});
});
Run Code Online (Sandbox Code Playgroud)
我知道嵌套订阅不是一个好的做法,我尝试使用flatMaporconcatMap但并没有真正了解如何实现这一点。