(角)如何在数组中查找字符串值

时间:2018-06-27 02:49:55

标签: arrays angular typescript service login

所以我想找到一个来自数组内部html输入的字符串值,我正在使用angular 5,并且正在使用组件内部的服务文件

我的代码

login.component.ts

export class LoginComponent implements OnInit {

  userData = [];

  constructor(private router: Router, private usersList: OperationService) {
  }

  ngOnInit() {
    this.usersList.usersData.subscribe(getData => {
      this.userData = getData;
    });
  }

  login(login: NgForm) {

    if (login.value.userfield === this.userData) {
      this.router.navigate(['home']);
    }
  }
}

operation.service.ts

export class OperationService {

  constructor() {}

  private users = new BehaviorSubject<any>([
    {
      name: 'admin',
      password: 'lol'
    }
  ]);

  usersData = this.users.asObservable();

}

我尝试了类似的方法:

this.userData.find(x => x.username == login.value.userfield )

this.userData.indexOf(login.value.userfield)

login.value.userfield === this.userData

JSON.stringify(this.userData)

但是这仍然行不通,有其他选择吗?我在丢东西吗?

1 个答案:

答案 0 :(得分:0)

您只有名字而没有用户名,

this.userData.find(x => x.name === login.value.userfield )