带参数的Angular2管道

时间:2017-03-24 14:14:42

标签: angular input pipe

我正在使用管道,我正在考虑使用带有字符串参数的管道作为@input组件值。像这样:

  <donut-chart [labels]="portfolio.currentStrategy.assetClasses | strategyFormat:'labels'" [values]="portfolio.currentStrategy.assetClasses | strategyFormat:'percentages'"></donut-chart>

我能这样做吗?

这是myPipe

@Pipe({name: 'strategyFormat'})
export class StrategyFormatPipe implements PipeTransform {
  transform(assetClasses: AssetClass[], type: string): any [] {
    let array: any [] = [];
    for (let i of assetClasses)
      if (type == 'labels')  array.push(i.name);
      else array.push(i.percentage);
    return array;
  }
}

我有这个错误:

  zone.js:516 Unhandled Promise rejection: Quotes are not supported for evaluation! ; Zone: <root> ; Task: Promise.then ; Value: 
ZoneAwareError {__zone_symbol__error: Error: Quotes are not supported for evaluation! at _AstToIrVisitor.visitQuote (http://localhost:…, __zone_symbol__stack: "Error: Quotes are not supported for evaluation!↵  …2 [<root>]↵    at Array.forEach (native) [<root>]", __zone_symbol__message: "Quotes are not supported for evaluation!"}
 Error: Quotes are not supported for evaluation!
    at _AstToIrVisitor.visitQuote (http://localhost:4200/vendor.bundle.js:18169:15) [<root>]
    at Quote.visit (http://localhost:4200/vendor.bundle.js:38853:24) [<root>]
    at ASTWithSource.visit (http://localhost:4200/vendor.bundle.js:39431:25) [<root>]
    at convertPropertyBinding (http://localhost:4200/vendor.bundle.js:17698:50) [<root>]
    at http://localhost:4200/vendor.bundle.js:81390:165 [<root>]
    at Array.forEach (native) [<root>]
    at bindDirectiveInputs (http://localhost:4200/vendor.bundle.js:81386:25) [<root>]
    at http://localhost:4200/vendor.bundle.js:81565:113 [<root>]
    at Array.forEach (native) [<root>]
    at ViewBinderVisitor.visitElement (http://localhost:4200/vendor.bundle.js:81563:24) [<root>]
    at ElementAst.visit (http://localhost:4200/vendor.bundle.js:7932:24) [<root>]
    at visit (http://localhost:4200/vendor.bundle.js:8213:37) [<root>]
    at http://localhost:4200/vendor.bundle.js:8215:42 [<root>]
    at Array.forEach (native) [<root>]

我也试过没有单引号。但这给了我同样的错误。

0 个答案:

没有答案