简体   繁体   English

ionic 3角度数据不会动态变化

[英]ionic 3 angular data is not changing dynamically

I am trying to create a login page that fetches user data from server and show error message for wrong login/password the code looks like this: 我试图创建一个从服务器获取用户数据的登录页面,并为错误的登录名/密码显示错误消息,代码如下所示:

login(x) {
    // alert(this.form.value)
  this.errmsg = "";
  if (x.uid == "" || x.pwd == "") {
    this.errmsg = "Fields should not be empty";
  }else{
    // parameter to pass on class
    var params={
      myclass:this,
    }
      //  Getrequest to get element by id 
    var GetUserById = {
      method: 'GET',
      url: 'http://192.168.136.136:8100/User/' + x.uid + '/',
      headers:
        {
          'Postman-Token': 'a81c98b6-5921-4ab5-bfbc-27dcff4b36ea',
          'Cache-Control': 'no-cache'
        }
      }
      var check=(body, x) =>{
        body=JSON.parse(body)
        if (body.length == 0) {
          return false
        }
        else {
          var uinfo = body[0]
          if (uinfo.Password == x.pwd) {
            window.sessionStorage.setItem('username', uinfo.UserId);
            window.sessionStorage.setItem('Role', this.defineRole(uinfo.Role));
            // window.sess

            return true
        }
        else{
          return false
        }
      }
    }
    var showError=() =>{
      params.myclass.errmsg= "Invalid Username or Password"
    }

    request(GetUserById, function (error, response, body) {
      if (error) throw new Error(error);
      if(check(body, x)){
        params.myclass.navCtrl.push(Home, {});
      }
      // createVerify
      else{
        showError()
        return
      }
    });
    }
 }

html : html:

<form [formGroup]="form" (ngSubmit)="login(form.value)">
  <ion-list>
    <ion-item>
      <ion-input type="text" formControlName="uid" placeholder="Username"></ion-input>
    </ion-item>
    <ion-item>
      <ion-input type="password" formControlName="pwd" placeholder="Password"></ion-input>
    </ion-item >
  </ion-list>
  <p id="almsg" class="text-right small" >{{errmsg}}</p>
  <div padding>
    <button ion-button type="submit" color="basic" block >Sign In</button>
  </div>
</form>

the errmsg in showerror() is not affecting the html page unless I tap on one of the input. 除非我点击其中一个输入,否则showerror()中的errmsg不会影响html页面。 I tried console.log but that is working perfectly fine, 我尝试过console.log,但是效果很好,

Try this, I guess you are not updating the class level variable errmsg with error message. 尝试此操作,我想您没有使用错误消息更新类级别变量errmsg

var showError=() =>{
  //params.myclass.errmsg= "Invalid Username or Password";
  this.errmsg = "Invalid Username or Password";
}

try this 尝试这个

var showError=() =>{
   this.errmsg = "Invalid Username or Password";
}

request(GetUserById, (error, response, body) => {
    if (error) throw new Error(error);
    if(this.check(body, x)){
        this.navCtrl.push(Home, {});
    }
    // createVerify
    else{
        this.showError()
        return
    }
});

First of all params.myclass.errmsg and this.errmsg are not same you need to change params.myclass.errmsg to this.errmsg then it will be show 首先, params.myclass.errmsgthis.errmsg不同,您需要将params.myclass.errmsg更改为this.errmsg,然后将其显示

Modify sohowError and request 修改sohowErrorrequest

var showError=() =>{
  this.errmsg= "Invalid Username or Password"
}


request(GetUserById, function (error, response, body) {
  if (error) throw new Error(error);
  if(check(body, x)){
    params.myclass.navCtrl.push(Home, {});
  }
  // createVerify
  else{
    showError();
    return false;
  }
});

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

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