我有2个文本框AuthorizeRep1Fname和AuthorizeRep1Lname。在将它放到AuthorizeRep1Name数据库的1列之前,我先将其组合在打字稿上。看下面的照片就是结果。
我正在使用它来注册和组合姓氏和名字。
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;
}
)
}
答案 0 :(得分:0)
您将数据作为两个字符串的组合发送,因此得到的结果是单个字符串。但是,当您在两个字符串的组合中插入空白时,您可以做的是在收到数据库的响应后,使用angular的 split()函数检查空白的出现。
假设您有两个字符串,分别为“嘿”“ Hello” ,那么数据库将具有嘿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**
快乐编码☻