如何将字符串传递给Angular 2(单击)函数?

时间:2016-08-09 11:27:43

标签: angular angular2-template

我的HTML中有一个div元素,如此

<div (click) = onPaginationClick()>next</div>

我的问题是:如何将字符串传递给onPaginationlick()函数?当我试图像这样传递它时:

<div (click)=onPaginationClick("myString")>next</div>

我有一个解析模板错误。

Enyone可以提供帮助吗?

3 个答案:

答案 0 :(得分:12)

如果整个表达式本身包含引号,则需要将整个表达式包装在引号中:

<div (click)="onPaginationClick('myString')">next</div>

答案 1 :(得分:2)

您需要使用嵌套引号:

<div (click)="onPaginationClick('helloworld')">next</div>

答案 2 :(得分:0)

component.ts

 "@angular/animations": "^4.1.1",
    "@angular/cli": "^1.0.3",
    "@angular/common": "^4.1.1",
    "@angular/compiler": "^4.1.1",
    "@angular/compiler-cli": "^4.1.1",
    "@angular/core": "^4.1.1",
    "@angular/forms": "^4.1.1",
    "@angular/http": "^4.1.1",
    "@angular/material": "^2.0.0-beta.3",
    "@angular/platform-browser": "^4.1.1",
    "@angular/platform-browser-dynamic": "^4.1.1",
    "@angular/platform-server": "^4.1.1",
    "@angular/router": "^4.1.1",

component.html

import { Component } from '@angular/core';

@Component({
  ...
})
export class AppComponent {
  ...
  onPaginationClick(value): void {
     ...
  }
}