简体   繁体   English

Angular 2 ngModel返回[对象]

[英]Angular 2 ngModel returning [object]

Just getting started with Angular 2 on an Ionic 2 project. 刚开始使用Ionic 2项目的Angular 2。 I have a simple login form: 我有一个简单的登录表单:

  <ion-list>
    <ion-item></ion-item>
    <ion-item>
      <ion-label fixed>Email</ion-label>
      <ion-input [(ngModel)]="loginData.Email" type="text" value=""></ion-input>
    </ion-item>
    <ion-item>
      <ion-label fixed>Password</ion-label>
      <ion-input [(ngModel)]="loginData.Password" type="password"></ion-input>
    </ion-item>
  </ion-list>

On the backend I have the object 在后端我有对象

export class LoginPage {
  user: User;
  local: Storage;
  loginData = { Email: null, Password: null};

  login() {
    console.log(this.loginData);
  }
}

When I type in the username and password though and try to access the data using a console.log the password is showing as: 当我输入用户名和密码并尝试使用console.log访问数据时,密码显示为:

Object {Email: "email - removed", Password: "[object Object]"}

Trying something like this.loginData.Password = String(this.loginData.Password); 尝试类似this.loginData.Password = String(this.loginData.Password); results in the same thing. 导致同样的事情。

This also results in the same thing console.log(JSON.stringify(this.loginData.Password)); 这也导致了相同的事情console.log(JSON.stringify(this.loginData.Password));

The issue is that ngModel is not accepting interpolation as an argument. 问题是ngModel不接受插值作为参数。

ngModel Documentation ngModel文档

your code should look like: 您的代码应如下所示:

<ion-input [(ngModel)]="loginData.Password" type="password"></ion-input> 

this seems to work just fine: 这似乎工作得很好:

<input type="text" [(ngModel)]="loginData.Email" >


<input type="password" [(ngModel)]="loginData.Password" >

<button (click)="login()">login</button>

login: 登录:

loginData = { Email:null, Password: null};

login() {
  console.log(this.loginData);
}

I was able to find a workaround. 我找到了一个解决方法。 I don't know why this works and the other doesn't, but if I store the values this way 我不知道为什么这样做而另一个没有,但如果我以这种方式存储值

  Email: string;
  Password: string;

And not in an object it worked fine. 而不是在一个对象它工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM