简体   繁体   English

如何在Angular 2中将值从一个字段解析并填充到另一个字段

[英]How to parse and populate the value from one field into an another in Angular 2

I have a simple form with two input fields. 我有一个带有两个输入字段的简单表单。 I would like to take the text from one field and snip the first part and replace it with some other text and populate it into the second field dynamically. 我想从一个字段中获取文本并剪裁第一部分,并用其他文本替换它,然后将其动态填充到第二个字段中。 I was able to achieve the getting part from the field with ease but I am not sure how to snip the value on keyup. 我能够轻松地从现场获得帮助,但是我不确定如何在keyup上截取值。 The following is the code: Component HTML 以下是代码:组件HTML

<div class="labels">Real Name</div> 
<div class="lbl">
     <input #realName type="text" [(ngModel)]="actualName" (keyup)="trigger()">
 </div>
 <div class="labels">Cyber Name</div>
 <div class="lbl">
     <input #cybName type="text"[(ngModel)]="cyberName"> 
  </div>

Component TS 组件TS

@ViewChild('realName') realName: ElementRef;
@ViewChild('cybName') cybName: ElementRef;

trigger() {
  this.cybName.nativeElement.value = this.realName.nativeElement.value;
}

On every keyup, I am setting the value of the cybName with the realName. 在每次键入命令时,我都使用realName设置cybName的值。 But, I would like to snip the first 4 characters of the realName and replace it with droid and the remaining characters of the realName ie, if the typed in real Name was "Alexys", I would like to make it "droidys". 但是,我想剪切realName的前4个字符,并将其替换为droid,然后再替换realName的其余字符,即,如果实名中键入的是“ Alexys”,我希望将其命名为“ droidys”。

I am sure I shouldn't be doing it with keyup but I am not sure what else to use for this situation. 我确定我不应该使用keyup来做到这一点,但是我不确定在这种情况下还有什么用。 Could anyone help me with it. 谁能帮助我。 Thank you. 谢谢。

you can use the splice to cut up your input. 您可以使用接头剪裁您的输入。 I am also assuming the following use cases 我还假设以下用例

"AAAAAA" -> "droidAA" “ AAAAAA”->“ droidAA”

"AAAA" -> "droid" “ AAAA”->“ droid”

"AA" -> "AA" “ AA”->“ AA”

const strLength = this.realName.nativeElement.value && this.realName.nativeElement.value.toString().length;

if (strLength >= 4) {
    this.cybName.nativeElement.value = 'droid' + this.realName.nativeElement.value.toString().splice(4, strLength);
} else {
    this.cybName.nativeElement.value = this.realName.nativeElement.value;
}

Also, you shouldn't have to use nativeElement . 另外,您不必使用nativeElement You should be able to use the variables themselves. 您应该能够自己使用变量。

if (this.actualName && this.acutualName.length >= 4) {
    this.cyberName = 'droid' + this.actualName.splice(4, this.acutualName.length);
} else {
    this.cyberName = this.actualName;
}

I think you just want to append the substr: 我认为您只想追加substr:

let cyberName = 'droid';
let realName = 'Alexys';
let cyberDroidName = `${cyberName}${realName.substring(4)}`;
console.log(cyberDroidName);

An easy solution that I think will solve your issue: 我认为一个简单的解决方案可以解决您的问题:

Changes in html: html中的更改:

<input #realName type="text" [ngModel]="actualName" (ngModelChange)="trigger($event)">

Changes in ts: ts的变化:

trigger(newActualName) {
  this.actualName = newActualName;
  this.cyberName = `Droid${event.substring(4)}`;
}

When you update the model ( actualName and cyberName ) it will also update the input with the new values. 当您更新模型( actualNamecyberName )时,它还将使用新值更新输入。

Also: Angular warns against using the ElementRef and accessing the DOM directly. 另外:Angular警告不要使用ElementRef并直接访问DOM。 See here for more info 在这里查看更多信息

Try the following: 请尝试以下操作:

<div class="labels">Real Name</div> 
<div class="lbl">
     <input #realName type="text" [(ngModel)]="actualName">
 </div>
 <div class="labels">Cyber Name</div>
 <div class="lbl">
     <input #cybName type="text"[ngModel]="'droid' + actualName.substring(4)"> 
  </div>

As you can see by all the answers there are many ways to skin this Cat. 正如您从所有答案中看到的那样,有很多方法可以使这只猫皮肤化。 If it were me, I would use a pipe to achieve this. 如果是我,我将使用管道来实现这一目标。 As shown in this working example . 如本工作示例所示。

You can create a pipe like this: 您可以这样创建管道:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'robotNamePipe'})
export class RobotNamePipe implements PipeTransform {
  transform(value: number, exponent: string): string {

  // Run your logic in here

    return `${value} - Anyhting you want!`;
  }
}

... and then in your HTML use it like this: ...然后在您的HTML中使用它,如下所示:

<div class="labels">Real Name</div> 
<div class="lbl">
     <input #realName type="text" [(ngModel)]="actualName">
 </div>
 <div class="labels">Cyber Name</div>

<p>Robot name: {{realName.value | robotNamePipe}}</p>

Because really, you probably don't want people to be able to edit the generated droid name anyway - do you? 因为实际上,您可能根本不希望人们能够编辑生成的机器人名称-是吗?

暂无
暂无

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

相关问题 Angular:如何以编程方式将值从一个字段复制到另一个字段? - Angular: how to programmatically copy a value from one field to another? AngularJs-如何使用angular.copy将值从一个输入字段取到另一个字段? - AngularJs - How can i fetch the value from one input field into another field using angular.copy? 如何使用 JQuery 从另一个页面上的文本字段值单击按钮时填充不同页面的文本字段? - How to populate a text field of a different page on button click from a text field value on another page using JQuery? 如何在appcelerator合金中将listview值从一个窗口解析到另一个窗口 - how to parse the listview value from one window to another in appcelerator alloy 将字段值从一个字段复制到另一个字段 - Copy field value from one field to another 以角度形式将一个输入字段值连接(附加)到另一个字段 - Concatenate (append) one input field value to another field in angular form 如何使用 JS 将值从一个输入字段复制到另一个字段 - How to Copy Value from one Input Field to Another Using JS 如何将值从一个输入字段复制到另一个 - How to Copy Value from one Input Field to Another 如何将一个变量中的键分配给另一个变量中的值,角度 - How to assign key from one variable to value in another, angular 单击提交按钮,将值从一个文本框填充到另一个文本框 - Populate value from one textbox to another on clicking on submit button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM