比较订阅Angular&中的数据可观察到的RXJS

时间:2018-05-22 10:53:14

标签: javascript arrays angular5

我是Angular&使用in-memory-web-api为Login POC写了下面的代码,

DB-service.service:

import { InMemoryDbService } from 'angular-in-memory-web-api';

export class InMemoryDataService implements InMemoryDbService {
  createDb() {
      let users = [
          {id: 1, username: 'user1', password:'user1', name:'John'},
          {id: 2, username: 'user2', password:'user2', name:'David'},
          {id: 3, username: 'user3', password:'user3', name:'Brad'},
          {id: 4, username: 'user4', password:'user4', name:'Jim'},
          {id: 5, username: 'user5', password:'user5', name:'Saun'}
      ];

      return {users};
  }
}

user.service:

import { loginUser } from '../_model/user';

@Injectable()

export class UserService {

  private headers = new Headers({ 'Content-Type': 'application/json' });
  private userUrl = 'api/users';  // URL to web api

  constructor(private http:  Http) {
  }


  getLogin(username: string, password: string) {
    const url = `${this.userUrl}`;    
    return this.http.get(url).map(res => res.json());
  }
}

模型/ user.ts

export class loginUser {
    constructor(
        public id: number,
        public username: string,
        public password: string,
        public name: string) { }
}

login.component.ts

login(form: FormGroup) {

    if (this.loginForm.valid) {
      this.userData.getLogin(form.value.loginUserTxt, form.value.loginPassTxt)
      .subscribe(
        data => {              
          console.log(data);  
           //comparing the data from the array in DB-service.service  
        },
        error => {              
          console.log("Fail");
        });

    }

我的问题是如何将订阅login.component.ts中的数据与输入值进行比较。

1 个答案:

答案 0 :(得分:1)

我已在订阅

中更新以下代码
this.loggedUser = this.userData.getAuthenticate(data, form.value.loginUserTxt, form.value.loginPassTxt)    
 if (this.loggedUser == "Invalid") {
      //error
 } else {
    //redirect
  }

全部谢谢