Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular RxJS——在使用RxJS时,如何通过单击按钮来触发计时器? @组件({ 模板:` 发送 {{seconds}}s `, 选择器:“密码重置表单” }) 导出类PasswordResetFormComponent实现OnInit{ 发送:布尔=假; 秒:数字=60; 反可观察=新主体(); 构造函数(公共authService:authService,公共路由器:路由器,公共formBuilder:formBuilder){ } 恩戈尼尼特(){ this.counterObservable.subscribe(()=>{ 这一秒--; },null,()=>{ 这个秒=60; this.sending=false; }); } 发送(){ this.sending=true; 这个。反可观测。下一个(可观测。间隔(1000)。取(60)); } }_Angular_Rxjs_Rxjs5 - Fatal编程技术网

Angular RxJS——在使用RxJS时,如何通过单击按钮来触发计时器? @组件({ 模板:` 发送 {{seconds}}s `, 选择器:“密码重置表单” }) 导出类PasswordResetFormComponent实现OnInit{ 发送:布尔=假; 秒:数字=60; 反可观察=新主体(); 构造函数(公共authService:authService,公共路由器:路由器,公共formBuilder:formBuilder){ } 恩戈尼尼特(){ this.counterObservable.subscribe(()=>{ 这一秒--; },null,()=>{ 这个秒=60; this.sending=false; }); } 发送(){ this.sending=true; 这个。反可观测。下一个(可观测。间隔(1000)。取(60)); } }

Angular RxJS——在使用RxJS时,如何通过单击按钮来触发计时器? @组件({ 模板:` 发送 {{seconds}}s `, 选择器:“密码重置表单” }) 导出类PasswordResetFormComponent实现OnInit{ 发送:布尔=假; 秒:数字=60; 反可观察=新主体(); 构造函数(公共authService:authService,公共路由器:路由器,公共formBuilder:formBuilder){ } 恩戈尼尼特(){ this.counterObservable.subscribe(()=>{ 这一秒--; },null,()=>{ 这个秒=60; this.sending=false; }); } 发送(){ this.sending=true; 这个。反可观测。下一个(可观测。间隔(1000)。取(60)); } },angular,rxjs,rxjs5,Angular,Rxjs,Rxjs5,嘿,我试着用ng2和RxJS制作一个计时器,当我点击发送按钮时,它会显示一个60秒的计时器,但是我花了很多时间,仍然不知道如何使用RxJS来做。 如果有人能帮助我,我将不胜感激 像这样的方法应该会奏效: @Component({ template: ` <div> <a *ngIf="!sending" (click)="send()">Send</a> <span

嘿,我试着用ng2和RxJS制作一个计时器,当我点击发送按钮时,它会显示一个60秒的计时器,但是我花了很多时间,仍然不知道如何使用RxJS来做。
如果有人能帮助我,我将不胜感激

像这样的方法应该会奏效:

@Component({
    template: ` <div>
                    <a *ngIf="!sending" (click)="send()">Send</a>
                    <span *ngIf="sending" >{{seconds}} s</span>
                </div>`,
    selector: 'password-reset-form'
})
export class PasswordResetFormComponent implements OnInit {

    sending:boolean = false;
    seconds:number = 60;    

    constructor(public authService:AuthService, public router:Router, public formBuilder:FormBuilder) {

    }

    ngOnInit() { }

    send() {

        this.sending = true;
        Observable.timer(1000, 60)
            .subscribe(() => {
                this.seconds--;
            }, 
            err => {},
            () => {
                this.sending = false;
                this.seconds = 60;
            });  
    }
}
@组件({
模板:`
发送
{{seconds}}s
`,
选择器:“密码重置表单”
})
导出类PasswordResetFormComponent实现OnInit{
发送:布尔=假;
秒:数字=60;
构造函数(公共authService:authService,公共路由器:路由器,公共formBuilder:formBuilder){
}
ngOnInit(){}
发送(){
this.sending=true;
可观察计时器(1000,60)
.订阅(()=>{
这是第二次;
}, 
err=>{},
() => {
this.sending=false;
这个秒=60;
});  
}
}

我这样解决了它。

这是错误的,我希望每60秒可以点击一次,而且它不能自动停止,但无论如何,谢谢你的回答。
send() {

    this.sending = true;
    let subscription = Observable.interval(1000).take(60).subscribe(()=> {
        this.seconds --;
    }, null, ()=> {
        this.seconds = 60;
        this.sending = false;
        subscription.unsubscribe();
    });
}