gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:58:24 24 4
gpt4 key购买 nike

请检查一下,我是否以正确的格式传递了updateId方法的li标签click函数的参数?控制台中的错误仅显示该行......

我的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>

我的 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;
}

}

它不工作。错误发生在从 jsonList 传递动态参数的 li 标记 click 函数方法上。所以请帮助我解决。

最佳答案

请勿在事件绑定(bind)(例如 (click) 属性)中使用 handle {{ }} - 它们将根据组件实例自动进行评估。并且也不需要单引号。像这样使用它:

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

关于javascript - Angular2 - 在进行下拉值选择时将动态加载的参数传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42419922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com