为岗位角4添加身体数据

时间:2017-08-03 00:22:58

标签: angular

我将输入元素中的内容添加到正文请求中。所以我可以根据会员ID和合作伙伴ID回退数据,我已经尝试了许多没有运气的事情,我们将不胜感激。我想使用2路数据绑定和模板驱动表单 我有FormsModule。 这是代码



import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

const guid = function () {
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
};

function s4() {
  return Math.floor((1 + Math.random()) * 0x10000)
    .toString(16)
    .substring(1);
}

@Component({
  selector: 'app-data',
  templateUrl: './data.component.html',
  styleUrls: ['./data.component.css']
})
export class DataComponent implements OnInit {
  memberId = 'SVR17066GYA';
  partnerId = 101;

  constructor(public http: Http) { }

  getMembers() {

    // const bodyGetInfo = {
    //   'PartnerId': this.partnerId,
    //   'MemberId': this.memberId,
    //   'Product': 'Default',
    //   'CorrelationId': guid()
    // };

    const bodyGetInfo = {
      'PartnerId' : this.partnerId,
      'MemberId' : this.memberId,
      'Product' : 'Default',
      'CorrelationId' : guid()
    };

    return this.http.post('url' )
      .map(res => res.json());
  }

  ngOnInit() {

    this.getMembers()
      .subscribe(members => {
        console.log(members);
      })
  }
}

<input type="text" [(ngModel)]="memberId" >
{{memberId}}
<input type="text" [(ngModel)]="partnerId">
{{partnerId}}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

所以你想在html中设置从服务器到输入的响应吗?

首先,您要在ngInit中发送请求,我想您在填写表单并单击“提交”按钮后发送请求,因此您需要从提交中发送(点击)事件的帖子按钮,您需要添加到您的HTML。如果我弄错了,请纠正我。

答案 1 :(得分:0)

好吧,我想通了,我想我会发布适用于模板驱动表单的新代码。我会在接下来建立反应形式,并在我得到它后也发布。希望这有助于其他人:)

import { Component } from '@angular/core';
import { Http } from '@angular/http';

const guid = function () {
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
};

function s4() {
  return Math.floor((1 + Math.random()) * 0x10000)
    .toString(16)
    .substring(1);
}

@Component({
  selector: 'app-data',
  templateUrl: './data.component.html',
  styleUrls: ['./data.component.css']
})
export class DataComponent {

  memberId: string;
  partnerId: number;

  constructor(public http: Http) {
  }

  submit(f) {
    console.log(f.value.memberId);
    console.log(f.value.partnerId);
    this.memberId = f.value.memberId;
    this.partnerId = f.value.partnerId;

    const bodyGetInfo = {
      'PartnerId': this.partnerId,
      'MemberId': this.memberId,
      'Product': 'Default',
      'CorrelationId': guid()
    };

    return this.http.post('url')
      .subscribe(res => {
        console.log(res.json())
      });
  }
}
<form novalidate #formData="ngForm" (ngSubmit)="submit(formData)">
  <input
    type="text"
    ngModel
    name="memberId">
    
  <input
    type="text"
    ngModel
    name="partnerId">
  <button class="btn btn-primary" type="submit">Submit</button>
</form>