【问题标题】:How can I get and separate data from database into to text box? Angular如何从数据库中获取数据并将其分离到文本框中?角
【发布时间】:2019-07-07 01:02:09
【问题描述】:

我有 2 个文本框 AuthorizeRep1Fname 和 AuthorizeRep1Lname。在将它放在数据库中的 1 列上,即 AuthorizeRep1Name 之前,我将它组合在 typescript 上。看看下面这张照片就是结果。

我正在使用它来注册和组合名字和姓氏。

this.Profile.AuthorizedRep1Name = (this.Profile.AuthorizedRep1FName + ' ' + 
this.Profile.AuthorizedRep1LName);
this.ProfileService.UpdateProfile(this.Profile).then(
  (response) => {
    console.log(this.Profile)
   }

这是我的 html。

<div class="row mx-auto">
<div class="col-sm-12 col-md-6 col-lg-6">
    <div class="input-container">
        <label for="Representative1">* First Name </label>
        <input name="r1fname" #r1fname="ngModel" id="Representative1Fname" type="text" [(ngModel)]="Profile.AuthorizedRep1FName" pInputText required/>
        <div class="errortxt" [hidden]="r1fname.valid || r1fname.pristine" class="alert alert-danger">
            <div class="errortxt" [hidden]="!r1fname.hasError('required')">First Name is Required!</div>
        </div>
    </div>
</div>

<div class="col-sm-12 col-md-6 col-lg-6">
    <div class="input-container">
        <label for="Representative1">* Last Name </label>
        <input name="r1lname" #r1lname="ngModel" id="Representative1Lname" type="text" [(ngModel)]="Profile.AuthorizedRep1LName" pInputText required/>
        <div class="errortxt" [hidden]="r1lname.valid || r1lname.pristine" class="alert alert-danger">
            <div class="errortxt" [hidden]="!r1lname.hasError('required')">Last Name is Required!</div>
        </div>
    </div>
</div>

我的问题是我无法将数据库中的数据分离并显示到我的两个文本框中,我只能在使用 ngModel AuthorizeRep1Name 而不是 AuthorizeRep1Fname 和 AuthorizeRep1Lname 时显示。 这是我的 get 函数。

GetProfileByID(UserID: any) {

this.ProfileService.GetProfileByID(UserID).then(
(response) => {
  this.Profile = response.Body;
  }
)
}

【问题讨论】:

  • 使其成为数据库中的两个字段。否则你需要拆分你的名字,但是你怎么知道在哪个位置拆分等等(双重名称会发生​​什么等等......)
  • 我无法对数据库进行任何更改,很多组件都会受到影响
  • 那你就不能把两个字段变成一个字段再变成两个字段了。或者您使用下面的拆分示例,但您会遇到很多其他问题(例如双重名称)

标签: html sql-server angular typescript


【解决方案1】:

您将数据作为两个字符串的组合发送,因此您得到的结果是单个字符串。但是,当您在两个字符串的组合中插入空白空间时,您可以做的是在收到数据库响应后使用角度的 split() 函数检查空白空间的出现。

假设你有两个字符串是 "hey" "Hello" 所以数据库会有 hey Hello。因此,在获得该字符串后将其存储到变量假设:

let stringSplit = this.Profile; // profile variable which will store response from database and which will have string **hey hello**

然后

let sepratedArray = stringSplit.split(" "); //where space is the separation character

然后检查 sepratedArray 的值作为数组索引

console.log(sepratedArray[0]); //will print **hey**

console.log(sepratedArray[1]); //will print **Hello**

快乐编码☻

【讨论】:

猜你喜欢
  • 2020-07-31
  • 2016-04-02
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多