小编edi*_*onv的帖子

Angular 4 - 取消订阅的最佳方式

我很好奇我如何取消订阅我的所有订阅.我知道takeWhile()和takeUntil().我发现takeUntil()对我来说更有用.

据我所知,takeWhile()在获取数据后生效.然后取消订阅直到组件被销毁.

什么是使用takeUntil()而不使用它的区别.只是.unsubscribe()?

不使用takeUntil()

ngOnInit() {
 this.subscriptions = this._membersService.getMembers().subscribe(data => 
 this.members = data)
}

ngOnDestroy() {
 this.subscriptions.unsubscribe();
}
Run Code Online (Sandbox Code Playgroud)

使用takeUntil

private destroyed$: Subject<{}> = new Subject();

ngOnInit() {
 this._membersService.getMembers().takeUntil(this.destroyed$).subscribe(data 
 => this.members = data)
}

ngOnDestroy() {
  this.destroyed$.next();
}
Run Code Online (Sandbox Code Playgroud)

我如何确定如果我成功取消订阅?

rxjs angular

9
推荐指数
1
解决办法
5690
查看次数

Angular 4 - HTTP服务404.找不到JSON文件

我是角度新手,我在http服务中遇到问题.我试过this.http.get(' http://jsonplaceholder.typicode.com/posts/1 ')作为样本数据,它可以工作.但当我使用this.http.get('src/data/employees.json')时,它显示404(未找到)

employees.service.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class EmployeesService {

    constructor(private http: Http) { }

    getEmployees() {
        this.http.get('src/employees.json')
            .map(response => response.json() )
            .subscribe(result => console.log(result) );
    }

}
Run Code Online (Sandbox Code Playgroud)

app.component.ts

import { Component } from '@angular/core';
import { EmployeesService } from './employees.service';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {

    constructor(private employeesService: EmployeesService){}

    ngOnInit() {
        this.employeesService.getEmployees();
    }

}
Run Code Online (Sandbox Code Playgroud)

angular-cli angular

8
推荐指数
1
解决办法
2万
查看次数

Angular 4 - 为输入添加值

我试图把值放到<input value="{{ val }}">但我觉得它错了,因为它返回空数据.

如何为输入设置默认值?

我已经创建了API以进行更新.我真的需要返回输入数据.

谢谢

angular

8
推荐指数
1
解决办法
2万
查看次数

Angular:Observable多次执行请求

我正在使用observable

服务

getMembers(): Observable<any[]> {
return this._http.get('http://localhost/membership/main/getUsers')
  .map(response => response.json() );
}
Run Code Online (Sandbox Code Playgroud)

零件

members$: Observable<any[]>;
ngOnInit() {
 this.members$ = this._membersService.getMembers()
}
Run Code Online (Sandbox Code Playgroud)

要求

-getUsers
-getUsers
Run Code Online (Sandbox Code Playgroud)

两者都返回相同的JSON数据

每次加载页面时,它都会返回重复的请求.这不是关于冷热要求.因为两个请求都返回相同的响应.但是当我删除了observable时,一切都还可以.只有一个请求

rxjs angular2-http angular

3
推荐指数
1
解决办法
2462
查看次数

Angular 4 - 取消选中时从数组中删除值复选框

取消选中复选框后,如何从数组中删除值?

HTML

<input type="checkbox" id="saveUserNameCheckBox" (change)="selChk(member.id)"  [checked]="false">
Run Code Online (Sandbox Code Playgroud)

零件

selChk(val) {

   if (this.id.indexOf(val) == -1) {
    this.id.push(val);
    console.log(this.id);
   }
}
Run Code Online (Sandbox Code Playgroud)

typescript angular

2
推荐指数
1
解决办法
4201
查看次数

标签 统计

angular ×5

rxjs ×2

angular-cli ×1

angular2-http ×1

typescript ×1