Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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 自定义角度结构指令仅适用于星号语法_Angular_Angular Template - Fatal编程技术网

Angular 自定义角度结构指令仅适用于星号语法

Angular 自定义角度结构指令仅适用于星号语法,angular,angular-template,Angular,Angular Template,我创建了一个Angular structural指令,但它只适用于*语法,而不适用于“expanded”语法 快速: 当您将星号放在结构指令前面,例如*ngIf=“xxx”时,模板编译器会将其展开,因此以下内容是等效的: <p *ngIf="condition"> Our heroes are true! </p> <ng-template [ngIf]="condition"> <p> Our heroes are true!

我创建了一个Angular structural指令,但它只适用于
*
语法,而不适用于“expanded”语法

快速:

当您将星号放在结构指令前面,例如
*ngIf=“xxx”
时,模板编译器会将其展开,因此以下内容是等效的:

<p *ngIf="condition">
  Our heroes are true!
</p>

<ng-template [ngIf]="condition">
  <p>
    Our heroes are true!
  </p>
</ng-template>

我们的英雄是真的!

我们的英雄是真的!

因此,我创建了自己的指令(基于ngIf源代码):

@指令({
选择器:“[featureBoxLoader]”
})
导出类功能BoxLoaderDirective{
private _thenTemplateRef:TemplateRef | null=null;
构造函数(私有_viewContainer:ViewContainerRef,
私有templateRef:templateRef)
{
这是.\u thenTemplateRef=templateRef;
}
恩戈尼尼特()
{
警惕(“是的,它能工作!”);
this._viewContainer.createEmbeddedView(this._thentTemplateRef,{$implicit:{numbers:[1,2,3]});
}
}
指令在我的模块中声明,当我像这样使用它时,它工作良好:

  <div *featureBoxLoader>
     This is my feature box
  </div>

这是我的功能盒
但是,当我这样做时,我会得到一个错误:

 <ng-template [featureBoxLoader] let-data></ng-template>

错误是:

无法绑定到“featureBoxLoader”,因为它不是的已知属性 “ng模板”。 1.如果“featureBoxLoader”是角度指令,则将“CommonModule”添加到此组件的“@NgModule.imports”中。 2.若要允许任何属性,请将“无错误模式”添加到此组件的“@NgModule.schemas”


发生了什么事?它显然找到了它,但我需要使用
let

语法,区别在于在
*ngIf
示例中,您提供了一个参数(以及相应的@Input参数)

如果您只是在没有条件的情况下应用指令,则不需要括号

所以正确的语法是:

<ng-template featureBoxLoader let-data></ng-template>

[ngIf]="condition"
<ng-template featureBoxLoader let-data></ng-template>