当我在模板中使用自定义管道时,就像这样.它运作良好.
{{user|userName}}
Run Code Online (Sandbox Code Playgroud)
是否可以在代码中使用管道?我试着这样用,
let name = `${user|userName}`;
Run Code Online (Sandbox Code Playgroud)
但它表明
userName未定义
我的替代方法是在代码中手动使用db.collection.findOne().但有什么聪明的方法吗?
我想将当前数据转换'yyyy-MM-dd'为.ts文件中的格式.我模板它可以通过使用数据管道轻松完成.如何在打字稿中做到这一点.
在模板中:
{{date | date:'yyyy-MM-dd'}}
Run Code Online (Sandbox Code Playgroud)
如何'yyyy-MM-dd'在打字稿中以这种格式转换.
现在我只是通过使用它来获取当前日期.
this.date = new Date();
Run Code Online (Sandbox Code Playgroud)
但需要将其转换为给定的格式.请指导如何做到这一点......谢谢!
在模板中使用日期,百分比和货币管道时遇到同样的问题 -
例如,我使用的是简单的百分比管道:
{{ .0237| percent:'1.2-2' }}
Run Code Online (Sandbox Code Playgroud)
它在Chrome上运行时有效,但是当我部署到iOS设备时,它会抛出此错误:
"EXCEPTION:ReferenceError:无法找到变量:[{{{{{0237 |百分比:'1.2-2'}}中的Intl ......"
有没有其他人遇到这个问题?任何建议或帮助将不胜感激!谢谢!
我有一个使用角度十进制管道的自定义十进制格式管道.此管道是共享模块的一部分.我在功能模块中使用它,并在运行应用程序时没有提供程序错误.
如果有任何拼写错误,请忽略.
./src/pipes/custom.pipe.ts
import { DecimalPipe } from '@angular/common';
..
@Pipe({
name: 'customDecimalPipe'
})
...
export class CustomPipe {
constructor(public decimalPipe: DecimalPipe) {}
transform(value: any, format: any) {
...
}
Run Code Online (Sandbox Code Playgroud)
./modules/shared.module.ts
import { CustomPipe } from '../pipes/custom.pipe';
...
@NgModule({
imports: [ .. ],
declarations: [ CustomPipe ],
exports: [ CustomPipe ]
})
export class SharedModule { }
Run Code Online (Sandbox Code Playgroud)
我在其中一个组件中注入自定义管道并调用transform方法来获取转换后的值.共享模块导入到功能模块中.
我想为基本的angular2管道添加一些额外的功能.
即.在货币管道上完成了一些额外的格式化.为此,我想在自定义管道的组件代码中使用现有管道.
有什么办法可以做到吗?
@Pipe({name: 'formatCurrency'})
export class FormatCurrency implements PipeTransform {
transform(value:number, args:string[]) : any {
var formatted = value/100;
//I would like to use the basic currecy pipe here.
///100 | currency:'EUR':true:'.2'
return 'Do some extra things here ' + formatted;
}
}
Run Code Online (Sandbox Code Playgroud) 从 Angular 9 开始,我们可以使用
$localize`Hello ${name}:name:`
Run Code Online (Sandbox Code Playgroud)
对于打字稿代码中的 i18n。这仍然有一些限制,因为该ng xi18n命令不检测字符串,但如果将这些文本手动添加到翻译文件中,它就可以工作。
该$localize函数在源代码的JSDoc 中有很好的记录,但是它没有解释如何使用复数。我的意思是这样的(伪代码):
$localize`Hello {${count}, plural, =1 {reader} other {readers}}`
Run Code Online (Sandbox Code Playgroud)
这可能$localize吗?如果是:如何?如果不是:Angular 如何将此类表达式从 HTML 编译为 TypeScript?
我想在我的组件中使用datePipe.我按照这里的说明,但我遇到了
Error: StaticInjectorError[DatePipe]:
StaticInjectorError[DatePipe]:
NullInjectorError: No provider for DatePipe!
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
零件
import { DatePipe } from '@angular/common';
export class LivePreviewComponent implements OnInit{
currentDate = new Date();
constructor(private datePipe:DatePipe) {}
ngOnInit() {
this.datePipe.transform(this.currentDate, 'YYYY-MM-DDTHH:mm')
}
}
Run Code Online (Sandbox Code Playgroud) 我试图DatePipe在我的Angular2应用程序中实例化一个对象,以便transform(...)在我正在开发的组件中使用函数.
// ...
import { DatePipe } from '@angular/common';
@Component({...})
export class PanelComponent implements OnInit {
// ...
datePipe: DatePipe = new DatePipe(); // Error thrown here
// ...
}
Run Code Online (Sandbox Code Playgroud)
这段代码在RC5中运行良好.我现在想升级到Angular2最终版本,当我跑收到此错误ng serve或者ng build,
~/tmp/broccoli_type_script_compiler-input_base_path-XitPWaey.tmp/0/src/app/panel/panel.component.ts (33, 24):
Supplied parameters do not match any signature of call target.
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?还有另一种实例化管道的方法吗?或者Angular是否停止支持在组件内部实例化Pipes?
在Angular2中,我们向组件添加注释以描述给定组件的元数据.我已经注意到,ComponentMetadata并ViewMetadata都具有templateUrl和template属性.ViewMetadata.template(或templateUrl)vs 之间的区别是什么ComponentMetadata.template,以及使用one over alt的实际用例是什么?
我想在Angular2的输入字段中添加一个数字管道.我正在使用模型驱动的表单,我的所有输入都有一个formControlName而不是使用数据绑定.我formControlName="number | number : '1.2-2'"遇到的问题是无效的代码.它抛出一个错误,说找不到formControlName.我不想删除formControlName来代替ngModel,因为我正在订阅表单输入以在使用表单时进行验证.