根据它的id获取绑定对象

时间:2018-04-28 06:20:27

标签: c# angular asp.net-web-api asp.net-core-mvc

我有public function update() { // Form data validation rules $this->form_validation->set_rules('title', 'Title', 'required', array('required' => 'The %s field can not be empty')); $this->form_validation->set_rules('desc', 'Short description', 'required', array('required' => 'The %s field can not be empty')); $this->form_validation->set_rules('body', 'Body', 'required', array('required' => 'The %s field can not be empty')); $this->form_validation->set_error_delimiters('<p class="error">', '</p>'); $id = $this->input->post('id'); if ($this->form_validation->run()) { $this->Posts_model->update_post($id, $data); redirect('posts/post/' . $id); } else { $this->edit($id); } } 个客户来搜索textbox

id

Enter Id: <input type="text" [(ngModel)]="temp.id"> <button (click)="myEvent()">My Button</button> <table class='table' *ngIf="collection"> <tbody> <tr *ngFor="let item of collection"> <td>{{ item.id }}</td> <td>{{ item.name }}</td> </tr> </tbody> 方法中,我不知道如何获取给定Id的相关绑定对象,以便我得到它的myEvent属性并将其发送到我的WEB API:

name

3 个答案:

答案 0 :(得分:1)

如果您有自己的项目集合,可以通过id属性找到一个项目。

以下是使用this的一种方式:

myEvent() {
    const item = this.collection.find(i => i.id === this.temp.id);

    if (!item) {
        return;
    }
    ...
}

答案 1 :(得分:0)

您可以使用JavaScript的本机方法find()来获取component.ts中的确切对象和访问名称字段

let name = this.collection.find(t=> t.id == this.temp.id).name; 

然后,您应该可以将您的名字传递给API。

答案 2 :(得分:0)

你可以这样做:

Enter Id: <input type="text" [(ngModel)]="temp.id"> 

<table class='table' *ngIf="collection">
<tbody>
   <tr *ngFor="let item of collection">
      <td>{{ item.id }}</td>
      <td>{{ item.name }}</td>
      <td><button (click)="myEvent(item)">My Button</button></td>
    </tr>
</tbody>

myEvent

中获取所选项目
myEvent(item: any) {
  this.http.get('url', {
      params: {
          id: item.id,
          name: item.name
    }
 })
}