简体   繁体   English

如何使用TypeScript将可选参数传递给Angular 2中的@Directives?

[英]How to pass optional parameter to @Directives in Angular 2 with TypeScript?

This is how you insert params into a directive. 这是将参数插入指令的方式。

<p [gridGroup]="gridGroup"></p>

But what I am wanting to do is make the param optional so that I won't have to always include it within my class. 但是我想做的是使参数成为可选参数,这样我就不必总是将其包含在班级中。

I am required to insert gridGroup in each class that references this html source. 我需要在引用此html源的每个类中插入gridGroup。

I have tried 我努力了

<p [gridGroup?]="gridGroup"></p>

and

<p [gridGroup]="gridGroup?"></p>

Both give me compile errors 两者都给我编译错误

在您的组件中,将gridGroup属性初始化为一个值,这样,如果未在父组件中声明该属性,则它仍然具有一个值/不会导致错误。

You can do something like this 你可以做这样的事情

@Input set gridGroup(value: any) {
      this._gridGroup= value;
      doSomething(value);
    }

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

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