没有值或方括号的角组件属性

发布于 2025-01-26 20:18:53 字数 736 浏览 1 评论 0原文

我需要将 isbold 属性添加到角度组件

<child-component isBold />

备注以下

  • 我更喜欢 not 在属性周围使用方括号[]名称如果用作true flag(如上所述,长版本为isbold = true)。

  • 但是,我希望能够将其用作输入属性

     &lt; child-component [isbold] =“ parentbooleanorfunction” /&gt;
     

代码bellow破坏了第一个要求

@Input() isBold: boolean = true

,在Angular 13中有办法无需编写其他助手功能吗?

i note 我读了类似的问题在这里,但是所选的解决方案需要我没有的其他代码或材料用法。

I need to add the isBold property to an Angular component

<child-component isBold />

Remark the following

  • I prefer do not use square brackets [] around the property name if used as true flag (as in the code above, long version would be isBold = true).

  • However I would like to be able to use it as an input property

    <child-component [isBold]="parentBooleanOrFunction" />
    

the code bellow breaks the first requirement

@Input() isBold: boolean = true

Is there a way, in Angular 13, to do it without writing additional helper functions?

I note I read the similar question here, but the selected solution needs additional code or Material usage I have not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇俏 2025-02-02 20:18:53

为此,我建议创建自己的cocion函数/类型类似于Angular CDK函数/类型(如果您不需要添加@Angular/cdk对于您的项目依赖项),比为每个输入创建一个指令非常容易实现:

例如, boolean属性来自@angular/cdk/cocrecion

/**
 * Type describing the allowed values for a boolean input.
 * @docs-private
 */
export type BooleanInput = string | boolean | null | undefined;

/** Coerces a data-bound value (typically a string) to a boolean. */
export function coerceBooleanProperty(value: any): boolean {
  return value != null && `${value}` !== 'false';
}

然后您可以直接在任何组件中使用它们,如以下内容:

@Input()
get isBold(): boolean {
  return this._isBold;
}
set isBold(isBold: BooleanInput) {
  this._isBold = coerceBooleanProperty(isBold);
}
private _isBold = false;
<child-component isBold />
<child-component [isBold]="parentBooleanOrFunction" />

您可以在Angular/codar/codions/src/cdk/coercion/下找到Angular CDK cocrecion cocrecion function/类型的完整列表:
https://github.com/github.com/angular/angular/compar/components/components/tree/main/main/main/main/ SRC/CDK/CORCION

更新:

如果您不想使用任何辅助功能/类型,则可以像 defted apis and themated apis and in angular 13

@Input() get isBold(): boolean {
  return this._isBold;
}
set isBold(value: boolean | '') {
  this._isBold = value === '' || value;
}
private _isBold = false;

当输入使用Getter/Setter对时,可能希望让Setter接受比Getter返回的类型更广泛的类型集,例如,当Setter首先转换输入值时。但是,直到打字稿4.3 getter/setter对具有相同的类型,因此无法准确声明此模式。

由于打字稿4.3限制已被删除;现在可以
接受比Getter返回的更宽类型。这意味着
不再需要该输入胁迫场,因为它们的效果可以
可以通过拓宽设置器的类型来实现。

在此处查看有关“输入设置器强制”折旧的更多信息:此处:
https://angular.io/guide/guide/guide/deprecations#input-settup-setter-coercion

To achieve that, I would suggest creating your own coercion functions/types similar to the Angular CDK functions/types (If you don't need to add the @angular/cdk to your project dependencies), which are very easy to implement than creating a directive for each Input:

For example, the Boolean Property from @angular/cdk/coercion:

/**
 * Type describing the allowed values for a boolean input.
 * @docs-private
 */
export type BooleanInput = string | boolean | null | undefined;

/** Coerces a data-bound value (typically a string) to a boolean. */
export function coerceBooleanProperty(value: any): boolean {
  return value != null && `${value}` !== 'false';
}

Then you can use them directly in any component, like the following:

@Input()
get isBold(): boolean {
  return this._isBold;
}
set isBold(isBold: BooleanInput) {
  this._isBold = coerceBooleanProperty(isBold);
}
private _isBold = false;
<child-component isBold />
<child-component [isBold]="parentBooleanOrFunction" />

You can find the full list of Angular CDK coercion functions/types under angular/components/src/cdk/coercion/:
https://github.com/angular/components/tree/main/src/cdk/coercion

UPDATE:

If you don't want to use any helper functions/types, you can handle it like the following as mentioned in Deprecated APIs and features at Angular 13:

@Input() get isBold(): boolean {
  return this._isBold;
}
set isBold(value: boolean | '') {
  this._isBold = value === '' || value;
}
private _isBold = false;

When a getter/setter pair is being used for the input it may be desirable to let the setter accept a broader set of types than what is returned by the getter, for example when the setter first converts the input value. However, until TypeScript 4.3 a getter/setter pair was required to have identical types so this pattern could not be accurately declared.

Since TypeScript 4.3 the limitation has been removed; setters can now
accept a wider type than what is returned by the getter. This means
that input coercion fields are no longer needed, as their effects can
be achieved by widening the type of the setter.

Read more about "Input setter coercion" deprecation in Angular 13 here:
https://angular.io/guide/deprecations#input-setter-coercion

吾家有女初长成 2025-02-02 20:18:53

它不会用作普通@Input

您可以使用指令来执行此操作,但它不会在 stackblitz

创建输入指令

@Directive({
  selector: '[isBold]'
})
export class IsBoldDirective {

  @Input() isBold:boolean;

  constructor() { }

  ngOnInit(){
    if(this.isBold!==false){
      this.isBold=true; // defaults to true
    }
  }
}

将其注入组件,

@Component({
  selector: 'app-testing',
  templateUrl: './testing.component.html',
  styleUrls: ['./testing.component.css']
})
export class TestingComponent {
  constructor(@Self() @Optional() private isBold:IsBoldDirective) { }
  get value(){
    return this.isBold?.isBold ?? true; // this 'true' is the default value if isBold is not there
  }
}

并将其像这样将其用于

Absence: <app-testing></app-testing>
Presence: <app-testing isBold></app-testing>
Set value: <app-testing [isBold]="false"></app-testing>

结果

Absence:

true
Presence:

true
Set value:

false 

You can use directive to do that but it will not work as an ordinary @Input

Working examople on stackblitz

Create input directive

@Directive({
  selector: '[isBold]'
})
export class IsBoldDirective {

  @Input() isBold:boolean;

  constructor() { }

  ngOnInit(){
    if(this.isBold!==false){
      this.isBold=true; // defaults to true
    }
  }
}

Inject it into the component

@Component({
  selector: 'app-testing',
  templateUrl: './testing.component.html',
  styleUrls: ['./testing.component.css']
})
export class TestingComponent {
  constructor(@Self() @Optional() private isBold:IsBoldDirective) { }
  get value(){
    return this.isBold?.isBold ?? true; // this 'true' is the default value if isBold is not there
  }
}

and use it like this

Absence: <app-testing></app-testing>
Presence: <app-testing isBold></app-testing>
Set value: <app-testing [isBold]="false"></app-testing>

with result

Absence:

true
Presence:

true
Set value:

false 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文