没有值或方括号的角组件属性
我需要将 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 beisBold = 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,我建议创建自己的
cocion
函数/类型类似于Angular CDK函数/类型(如果您不需要添加@Angular/cdk
对于您的项目依赖项),比为每个输入创建一个指令非常容易实现:例如, boolean属性来自
@angular/cdk/cocrecion
:然后您可以直接在任何组件中使用它们,如以下内容:
您可以在
Angular/codar/codions/src/cdk/coercion/
下找到Angular CDKcocrecion
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 :
在此处查看有关“输入设置器强制”折旧的更多信息:此处:
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
:Then you can use them directly in any component, like the following:
You can find the full list of Angular CDK
coercion
functions/types underangular/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:
Read more about "Input setter coercion" deprecation in Angular 13 here:
https://angular.io/guide/deprecations#input-setter-coercion
它不会用作普通@Input
您可以使用指令来执行此操作,但它不会在 stackblitz
创建输入指令
将其注入组件,
并将其像这样将其用于
结果
You can use directive to do that but it will not work as an ordinary @Input
Working examople on stackblitz
Create input directive
Inject it into the component
and use it like this
with result