简体   繁体   English

Angular2-在执行下拉值选择时将动态加载的参数传递给函数

[英]Angular2 - pass dynamically loaded parameters to function while doing dropdown value selection

Please check that, I am passing the parameters of li tag click function of updateId method in the correct format or not ? 请检查是否以正确的格式传递了updateId方法的li标签点击函数的参数? The errors in the console shows that line only.... 控制台中的错误仅显示该行。

My app.component.html code is 我的app.component.html代码是

<div class='form-group' style="width: 100%;">
    <div class="btn-group" style="width: 100%;">
        <button type="button" class="btn btn-default" style="width : 75%;text-align: left;">{{name}}</button>
        <button type="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle"><span class="caret"></span></button>
        <ul class="dropdown-menu" style="width: 75%;">
             <li *ngFor="let result of jsonList" ><a class="dropdown-item" (click)="updateId('{{result.id}}', '{{result.name}}')" >{{result.name}}</a>
            </li>
        </ul>
    </div>
</div>

My app.component.ts is , 我的app.component.ts是,

import { HomeService } from '../../services/home.service';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home-component',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  id: string;
  name : string;
  jsonList : any;

  constructor(private homeservice: HomeService) { }

  ngOnInit() {
    this.id = null;
    this.name = "SELECT";
    this.jsonList= [{"Id" : "F1", "name" : "FONE"}, {"id" : "F2", "name" : "FTWO"}]
  }



  updateId(id : string, name : string) : void {
        this.id = id;
        this.name = name;
  }

}

It is not working. 它不起作用。 The error is on the li tag click function method passing the dynamic parameters from the jsonList . 错误在于li标记单击函数方法从jsonList传递动态参数。 So pls help me to get resolve. 因此,请帮助我获得解决方案。

Don't use the handlebars {{ }} in event bindings such as the (click) attribute - they will be evaluated against the Component instance automatically. 不要在事件绑定(例如, (click)属性(click)使用把手{{ }} -它们将自动针对Component实例进行评估。 and the single quotes are not needed either. 并且也不需要单引号。 Use it like so: 像这样使用它:

<li *ngFor="let result of jsonList">
    <a (click)="updateId(result.id, result.name)">{{result.name}}</a>
</li>

You don't need {{ }} while specifying arguments to an event handlers (ng-click). 在为事件处理程序(ng-click)指定参数时,不需要{{}}。 The correct syntax would be 正确的语法是

<li *ngFor="let result of jsonList">
    <a (click)="updateId(result.id, result.name)">{{result.name}}</a>
</li>

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

相关问题 Angular2 - 将参数传递给动态生成的组件? - Angular2 - Pass parameters to a dynamically generated component? 我如何发送选择时的下拉值作为参数传递给同一控制器中的另一个函数; 角 - How can I send the dropdown value on selection to pass as a parameter to another function within the same controller; angular 在选择时将下拉值从 function 传递给 React.useEffect() - Pass dropdown value from function to React.useEffect() on selection Angular2 单元测试选择下拉值 - Angular2 unit test select dropdown value 如何在没有ngClick的情况下将值作为参数传递给Angular2中的onclick内的函数调用? - How to pass values as parameters to a function call inside onclick in Angular2 without ngClick? Jquery - 动态地将参数传递给函数 - Jquery - Dynamically pass parameters to function Angular2将函数参数传递给构造函数 - Angular2 pass function argument to constructor 如何将后端渲染的参数传递给angular2 bootstrap方法 - How to pass parameters rendered from backend to angular2 bootstrap method Angular2输入在键入时仅接受数字,但在执行复制粘贴时不接受数字 - Angular2 input accept only number when typing, but not doing the same while doing copy paste 在运行时选择下拉列表时动态地填充Angular Populate下拉选项 - Angular Populate dropdown options dynamically while selecting dropdown in runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM