简体   繁体   English

在Angular Filter表达式中使用管道字符

[英]Use the pipe character in an Angular Filter expression

i would like to use the '|' 我想使用'|' character in an Angular 1 Filter expression lie Angular 1 Filter表达式中的字符

{{ ctrl.items | map: 'name|id' | join: ',' }}

Is there some kind of character escaping i could use? 我可以使用某种转义字符吗? I know that the | 我知道 character is used for calling a Filter, but i would like to use it to concat the two properties 'name' and 'id'. 字符用于调用过滤器,但我想用它来连接两个属性“名称”和“ id”。

And yes, i know that i could write a function in the controller to concatenate the two properties but i'm interested if there is a way to do this in the expression. 是的,我知道我可以在控制器中编写一个函数来连接这两个属性,但是我对是否有一种在表达式中执行此操作的方法感兴趣。

PS: The filter map and join are from this repo: https://github.com/a8m/angular-filter PS:过滤器映射和联接来自此仓库: https : //github.com/a8m/angular-filter

Update : 更新

In the controller: 在控制器中:

ctrl.items = [{ name: 'ape', id:1 }, { name: 'john', id:2 }];

In the template: 在模板中:

<input type='hidden' value="{{ ctrl.items | map: 'name|id' | join: ',' }}" >

expected output: 预期输出:

<input type='hidden' value="ape|1,john|2" >

You have to create a custom filter to form the value as mentioned by you. 您必须创建一个自定义过滤器以形成您提到的值。

var app = angular.module('myApp', []);
app.filter('map', function() {
  return function(input, propName) {
  var prop = propName.split("|");
  return input.map(function(item) {
  return item[prop[0]] +"|"+ item[prop[1]];
   });
  };
});

and then your input text as 然后输入文本为

<input type="text" value="{{(ctrl.items | map:'name|id').join(',')}}"/>

Please find the working Plnkr Working Plnkr 请找到工作的Plnkr 工作的Plnkr

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

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