简体   繁体   English

将ID(Index)传递给onEditDetail()参数

[英]Pass ID(Index) to onEditDetail() Parameters

How can i pass the value index or id to EditDetail() parameters. 如何将值索引或ID传递给EditDetail()参数。 I want it to be put inside the parenthesis of onEditDetail() once i click the onEditDetail() in html 我希望将其放置在onEditDetail()的括号内,方法是单击html中的onEditDetail()

ngOnInit() {
  this.route.params
    .subscribe((params: Params) => {
      this.id = +params['id'];
      this.user = this.userService.getUser(this.id);
    });
}

onEditDetail(id: string) {
  console.log(id);
}

user-detail.component.html 用户detail.component.html

 <div class="container">
  <div class = "row">
    <div class="col-md-3"></div>
    <div class = "col-md-8">
      <div class="card">
        <div class="card-header">
          {{ user.l_name}} 's Complete Information<span class="pull-right"><input class="form-control" placeholder="Search User" name="srch-term" id="srch-term" type="text"></span>
        </div> 
        <table class="table table-responsive">
          <thead class="thead-default">
            <tr>
              <th>First Name</th>
              <th>Last Name</th>
              <th>Contact Number</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>{{ user.f_name }}</td>
              <td>{{ user.l_name }}</td>
              <td>{{ user.contact_no }}</td>
              <td><button class=" btn btn-primary" style="cursor: pointer;" (click)="onEditDetail()">Edit</button> </td>
            </tr>
          </tbody>
        </table>     
      </div>
    </div>
  </div>
</div>

Adding to other answers, 除了其他答案,

You need to understand that you are making an asynchronous call. 您需要了解自己正在进行异步调用。

So If you need to access id anywhere else, you need to get that on completion of asynchronous call, not before that. 因此,如果您需要在其他任何地方访问id,则需要在异步调用完成时获取它,而不是在此之前获取它。

And this is reason, you need to call onEditDetail() inside Subscribe block as last parameter. 这就是原因,您需要在Subscribe块内调用onEditDetail()作为最后一个参数。 there are 3 params to subscribe. 有3种参数可供订阅。

  1. Data which is returned by call, 通过调用返回的数据,
  2. Error if any occured. 如果发生任何错误。
  3. On complete if you wish to do something. 如果您想做些事情,请完成。

In your case, as 3rd parameter, you have to call onEditDetail method 对于您的情况,作为第三个参数,您必须调用onEditDetail方法

ngOnInit() {
  this.route.params
    .subscribe((params: Params) => {
      this.id = +params['id'];
      this.user = this.userService.getUser(this.id);

      // 3rd parameter, this is where call is subscribe is completed
      this.onEditDetail(this.id);
    });
}

You can do that 你可以那样做

ngOnInit() {
  this.route.params
    .subscribe((params: Params) => {
      this.id = +params['id'];
      this.user = this.userService.getUser(this.id);

      // Your method call
      this.onEditDetail(this.id);
    });
}

onEditDetail(id: string) {
  console.log(id);
}
ngOnInit() {
  this.route.params
    .subscribe((params: Params) => {
      this.id = +params['id'];
      this.onEditDetail(this.id); //call onEditDetails and pass id to it.
      this.user = this.userService.getUser(this.id);
    });
}

onEditDetail(id: any) {
  console.log(id);
}

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

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