Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 有人知道如何在component.ts文件中使用titlecase来代替html文件吗?_Angular_Typescript_Title Case - Fatal编程技术网

Angular 有人知道如何在component.ts文件中使用titlecase来代替html文件吗?

Angular 有人知道如何在component.ts文件中使用titlecase来代替html文件吗?,angular,typescript,title-case,Angular,Typescript,Title Case,我尝试了以下代码,但不起作用 import { Component } from '@angular/core'; import { TitleCasePipe } from '@angular/common'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ] }) export class AppComponent

我尝试了以下代码,但不起作用

import { Component } from '@angular/core';
import { TitleCasePipe } from '@angular/common';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  title = 'Working with Pipe';
  testValue = this.titleCasePipe.transform('firstletter should be upper case.');


  constructor(
    public titleCasePipe: TitleCasePipe
  ){}

}

在组件元数据的提供者数组中添加TitleCasePipe:

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
providers: [TitleCasePipe]
})

在组件元数据的提供者数组中添加TitleCasePipe:

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
providers: [TitleCasePipe]
})

像这样将代码放入构造函数中

 testValue: string;


 constructor(
    public titleCasePipe: TitleCasePipe
 ){
    this.testValue = this.titleCasePipe.transform('firstletter should be upper case.');
 }

像这样将代码放入构造函数中

 testValue: string;


 constructor(
    public titleCasePipe: TitleCasePipe
 ){
    this.testValue = this.titleCasePipe.transform('firstletter should be upper case.');
 }

您可以只创建一个对象,或者像@Gérôms所说的那样添加一个提供程序

  title = 'Working with Pipe';
  titleCasePipe=new TitleCasePipe()
  testValue = this.titleCasePipe.transform('firstletter should be upper case.');

  constructor(){
  }

您可以只创建一个对象,或者像@Gérôms所说的那样添加一个提供程序

  title = 'Working with Pipe';
  titleCasePipe=new TitleCasePipe()
  testValue = this.titleCasePipe.transform('firstletter should be upper case.');

  constructor(){
  }
没有管道 这是你用滴定酶的简单溶液

您可以像这样直接使用,而不必添加额外的代码,因为它已经提供了这些特性

HTML

用烟斗 您也可以像这样签入输入字段

declarations: [ TitleCasePipe ],
HTML

别忘了添加这样的声明

declarations: [ TitleCasePipe ],
这是我不带烟斗的stackBlitz

这是你用滴定酶的简单溶液

您可以像这样直接使用,而不必添加额外的代码,因为它已经提供了这些特性

HTML

用烟斗 您也可以像这样签入输入字段

declarations: [ TitleCasePipe ],
HTML

别忘了添加这样的声明

declarations: [ TitleCasePipe ],

这是我的stackBlitz

转换函数返回转换后的参数。因此,如果不使用该函数初始化局部变量,也不需要将其放入构造函数中,那么调用该函数将是无用的对不起,我已经更新了代码,不管怎么说,为什么你需要在构造函数或ngOnInit中包含这些代码,是因为到那时TitleCasePipe可以使用了。你能分享你的stackblitz链接吗?你不需要在构造函数或ngOnInit中加入证据:transform函数返回转换后的参数。因此,如果不使用该函数初始化局部变量,也不需要将其放入构造函数中,那么调用该函数将是无用的对不起,我已经更新了代码,不管怎么说,为什么你需要在构造函数或ngOnInit中包含代码,是因为到那时TitleCasePipe可以使用了,你能分享你的stackblitz链接吗?你不需要在构造函数或ngOnInit中加入证据:它在Angular 9中是可注入的:这也是正确的,但我更喜欢选择第一个答案,不管怎样,谢谢你的回答。它是可注射的角度9:这也是正确的,但我宁愿选择第一个答案,不管怎样,谢谢你的回答。请在下面签出我的答案请在下面签出我的答案