简体   繁体   English

如何将Angular 2表单输入传递给TypeScript组件?

[英]How to pass Angular 2 form input to typescript component?

I am new to angular 2.I am looking for a way to pass the form input values to the ts component ,to send to spring controller as a JSON string.The basic form I have created is as follows 我是angular 2的新手,我正在寻找一种将表单输入值传递给ts组件,以JSON字符串形式发送到spring控制器的方法。我创建的基本表单如下

person.component.html person.component.html

    <div class="panel-body">
    <div class="row">   
    <div class="col-sm-1"><label class="control-label" for="Name">Full Name:
   </label></div>
    <div class="col-sm-3">
    <input type="text" class="form-control2" placeholder="Full Name" 
    [(ngModel)]="user.name"></div></div>  
    <br>
    <div class="row">
    <div class="col-sm-1"><label class="control-label" for="Email">Email:</label></div>
        <div class="col-sm-3">
            <input type="text" class="form-control2"  [(ngModel)]="user.email">
        </div>
   </div>
   <br>
    <div class="col-sm-9">              
                <button class="btn1" (click)="savePage()"><b>Save</b></button>
            <button class="btn1" (click)="resetPage()"><b>Cancel</b></button>
                </div>  </div>

person.ts person.ts

export class Person {
    constructor(
        public name: string,
        public email: string,
            ) {  }
}

What is to be done further? 下一步要做什么?

You are passing it already! 您已经通过了! [(ngModel)]="user.name" - this is so called two way binding. [(ngModel)]="user.name" -所谓的双向绑定。 Now, in your typescript file, object user is filled with your inputs' values Now you would need some kind of service, ie user.service.ts which needs to be specified in your Component's providers. 现在,在您的打字稿文件中,对象user填充了输入的值。现在,您需要某种服务,即user.service.ts ,需要在组件的提供程序中指定。

//component //零件

@Component({
  [...]
  providers: [UserService]
})


export class SomeComponent {

  user: User = new User();

  public SomeComponent(private _userService: UserService) {}

  public addUser(): void {
    this._userService.addUser(this.user).subscribe(res => {
      console.log(res); //whatever
    });
  }
}

//service //服务

public addUser(user: User) {
    return this.http.post(this.host + this.apiPrefix + '/users/', JSON.stringify(user),
      {headers: <your headers>})
    .map((res: any) => {
      return res;
    });
  }

If you are using [(ngModel)] your Person.ts look like this 如果您使用的是[(ngModel)] Person.ts看起来像这样

export class Person {
    name: string;
    email: string; 
}

then import it in person.component.ts 然后将其导入person.component.ts

import { Person  } from 'person.ts';

and inside your class PersonComponent declare user:Person; 在您的类PersonComponent声明user:Person; then call this.user = new Person(); 然后调用this.user = new Person(); like this in constructor 像这样在constructor

now [(ngModel)] will map the data for you. 现在[(ngModel)]将为您映射数据。

In TypeScript you can do things like: 在TypeScript中,您可以执行以下操作:

export class Person {
  name: string;
  email: string;
}

const email = 'some mail';
const name = 'some name';
const person: Person = { name: name , email:email }

In your case it would be: 您的情况是:

const person: Person = {name: user.name, email: user.email}

or just 要不就

const person: Person = user;

In the end you just need to make sure, that the properties match. 最后,您只需要确保属性匹配即可。 If you have, for example, a methode like this: 例如,如果您有这样的方法:

 someMethod(input: Person): void {}

You can pass your user object as an parameter: 您可以将用户对象作为参数传递:

 someMethod(user); <--- this will work;

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

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