Typescript 如何在Angular5中使用click事件初始化组件
我想在单击div时初始化一个组件,下面是我的代码 在html文件中Typescript 如何在Angular5中使用click事件初始化组件,typescript,angular5,Typescript,Angular5,我想在单击div时初始化一个组件,下面是我的代码 在html文件中 <li *ngFor="let contact of contactList"> <div (click)="anyFunctionName($event,contact.id)"> </div></li> <component-selector></component-selector> 在ts文件中 anyFunctionName(event,
<li *ngFor="let contact of contactList">
<div (click)="anyFunctionName($event,contact.id)">
</div></li>
<component-selector></component-selector>
在ts文件中
anyFunctionName(event,id):any{
// I will do some stuff,
and output will be goes to another component html file,
that will call another component <component-selector></component-selector> and display the output there
}
anyFunctionName(事件,id):任意{
//我会做一些事情,
输出将转到另一个组件html文件,
它将调用另一个组件并在那里显示输出
}
有什么想法吗
谢谢,您可以为组件创建一个属性 ts文件
anyFunctionName(event,id):any{
this.myVar = id;
}
html文件
<li *ngFor="let contact of contactList">
<div (click)="anyFunctionName($event,contact.id)">
</div></li>
<component-selector></component-selector>
<component-selector [myVar]="myVar"></component-selector>
您可以在
角型芯
中导入输入
。希望有帮助。在组件中添加一个布尔变量并将其初始化为false,如下所示:
导入{
组成部分,
奥尼特
}从“@angular/core”开始;
@组成部分({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent实现OnInit{
showthatDiv:boolean;
恩戈尼尼特(){
this.showthatDiv=false;
}
anyFunctionName(事件,id):任意{
this.showthatDiv=true;
}
公共联系人列表:任意[]=[{
“id”:1,
“名称”:“名称1”
}, {
“id”:2,
“名称”:“名称2”
}, {
“id”:3,
“名称”:“名称3”
}];
}
在html中,您只需绑定该属性:
{{contact.name}
你好
我不确定这是否是您想要的,但请尝试阅读以下内容: