如何從窗體獲取值。我曾嘗試下面的代碼:如何在angular2中獲取窗體
import {Component} from "@angular/core";
import {AuthService} from "./AuthService";
interface Credentials {
username: string,
password: string
}
@Component({
selector: 'login',
template: `
<form #f="ngForm" (ngSubmit)="onLogin(f.value)" *ngIf="!auth.loggedIn()">
<input type="text" placeholder="username" ngControl="username">
<input type="password" placeholder="password" ngControl="password">
<button type="submit">Submit</button>
</form>
`
})
export class LoginComponent {
credentials: Credentials;
constructor(private auth: AuthService) {}
onLogin(credentials) {
console.log("username"+credentials.username,"password"+credentials.password);
this.auth.login(credentials);
}
}
您是否收到任何錯誤? –