控制台打印错误:
Uncaught ReferenceError: onSelect is not defined at HTMLAnchorElement.onclick (VM998 :14)
我在.html文件中的列表数据
<ul class="nav navbar-nav"> <li><a href="#" onclick="onSelect('Recipe')">Recipe</a></li> <li><a href="#" onclick="onSelect('Shoppinglist')" id="ShoppingOnSelect">Shopping List</a></li> </ul>
在调用此函数,Angular js中的My Typescript文件时,
export class HeaderComponent{ @Output() featureSelected = new EventEmitter<String>(); onSelect(feature: string){ console.log(feature) this.featureSelected.emit(feature); } }
当我单击onclick时,Onselect函数本身未在控制台中运行
解决方案
It should be (click)
<li><a href="#" (click)="onSelect('Recipe')">Recipe</a></li>
If you want to prevent the href=”#” reloading the page:
<li><a href="#" (click)="!!onSelect('Recipe')">Recipe</a></li>
And also you can use replace href=”#” with [routerLink]=”” which is an alternative
<li><a [routerLink]="" (click)="!!onSelect('Recipe')">Recipe</a></li>