Code Guru
Code Guru

Reputation: 15578

how to manipulate data of observable type in angular2

I am new angular2 so please ignore if you find it the basic question.

I have a variable of type Observable

users: Observable<User[]>;

and in the constructor, I am filling it

constructor(private roleService: RoleService, private userService: UserService) {
        this.users = this.userService.getAllUsers();
}

Now I want to manipulate/iterate the data of this.users. How can I do that?

Upvotes: 0

Views: 620

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657108

You can use map or any other of the long list of available operators.

You need to subscribe to execute the observable, otherwise it won't do anything. You can also use .forEach(...), .toArray(), (and others) instead of subscribe():

constructor(private roleService: RoleService, private userService: UserService) {
        this.users = this.userService.getAllUsers()
        .map(val => val + 'xxx')
        .subscribe(val => console.log(val));
}

See also https://blog.thoughtram.io/angular/2016/01/06/taking-advantage-of-observables-in-angular2.html

Upvotes: 1

Related Questions