我正在使用BackendLess構建Ionic項目。但我似乎無法使用打字稿文件中的HTML元素的ID。在離子2中使用getElementById
我在代碼中使用了@ViewChild。
HTML代碼:
<ion-item>
<ion-label floating>Name</ion-label>
<ion-input type="text" id="name"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Email</ion-label>
<ion-input type="email" id="email"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Password</ion-label>
<ion-input type="password" id="password"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Phone Number</ion-label>
<ion-input type="text" id="phone"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Profile Picture</ion-label>
<ion-input type="file" id="pic"></ion-input>
</ion-item>
<button ion-button (click)='userreg();'>Register</button></ion-content>
打字稿代碼:
export class Register {
@ViewChild('name') name;
@ViewChild('email') email;
@ViewChild('password') password;
@ViewChild('phone') phone;
@ViewChild('pic') pic;
constructor(public navCtrl: NavController, private alertCtrl: AlertController) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad Register');
}
userreg() {
var picURL;
var user = new Backendless.User();
user.email=this.email;
user.password=this.password;
user.name=this.name;
user.phone=this.phone;
Backendless.UserService.register(user).then(this.userRegistered).catch(this.gotError);
}
gotError(err) // see more on error handling
{
console.log("error message - " + err.message);
console.log("error code - " + err.statusCode);
}
userRegistered(user)
{
console.log("User registered");
}
}
的@ViewChild似乎並沒有越來越值。我檢查了控制檯上的輸出,並顯示「Undefined」作爲輸出,因此不允許註冊到BackendLess中。
請勿將鏈接發佈到外部來源。因爲它們可能會脫機,並且未來的用戶無法訪問它們。相反,只需將相關作品粘貼到問題中即可。 – Red