简体   繁体   English

如何将click事件绑定到angular2模板插值?

[英]How can I bind click event to an angular2 template interpolation?

Is it possible to bind click event to an interpolation? 是否可以将click事件绑定到插值? because when I am trying to execute the following code I get the following 因为当我尝试执行以下代码时,我得到以下信息

ERROR Error: Uncaught (in promise): 错误错误:未捕获(承诺中):

Error: Template parse errors: 错误:模板解析错误:

Parser Error: Got interpolation ({{}}) where expression was expected at column 7 in [upload({{config.fileLocation}})] 解析器错误:进行了插值({{}}),该表达式在[upload({{{config.fileLocation}})]的第7列处应有表达式

this is the template where we have faulty interpolation, 这是我们插值错误的模板,

  <mat-card-actions>
      <button mat-raised-button type="button" (click)="upload({{config.fileLocation}})">Upload</button>
  </mat-card-actions>

and this is the angular component which is desired to be executed. 这是希望执行的角度分量。

  upload(location = "/tmp/") {
  this.loader.open();

  const fileBrowser = this.fileInput.nativeElement;
  if (fileBrowser.files && fileBrowser.files[0]) {
    const formData: FormData = new FormData();
    formData.append('file', fileBrowser.files[0]);
    formData.append('data', JSON.stringify({
      "method": "filesystem.put",
      "params": [location + fileBrowser.files[0].name, { "mode": "493" }]
    }));

    this.http.post(this.apiEndPoint, formData).subscribe(
      (data) => {
        this.loader.close();
        this.snackBar.open("your files are uploaded", 'close', { duration: 5000 });
      },
      (error) => {
        this.loader.close();
        this.dialog.errorReport(error.status, error.statusText, error._body);

      }
    );
  };
}

Just 只是

(click)="upload(config.fileLocation)"

Never use {{}} together with [foo]="..." or (bar)="..." 切勿将{{}}[foo]="..."(bar)="..."

[] and () already mark the attribute as Angular binding. []()已经将该属性标记为Angular绑定。 {{}} also only allows string binding (stringifies every value), while the others allow binding of object values. {{}}也仅允许字符串绑定(将每个值字符串化),而其他则允许对象值的绑定。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM