所以我想找到一个来自数组内部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)
但是这仍然行不通,有其他选择吗?我在丢东西吗?
答案 0 :(得分:0)
您只有名字而没有用户名,
this.userData.find(x => x.name === login.value.userfield )