【问题标题】:Using $implict to pass multiple parameters使用 $implicit 传递多个参数
【发布时间】:2018-07-09 10:46:17
【问题描述】:

我有一个递归模板,类似于以下内容:

<ng-container *ngTemplateOutlet="testTemplate; context: {$implicit:jsonObj1}">
</ng-container>

<ng-template #testTemplate let-Json1>
</ng-template>

它工作正常,我使用 $implicit 发送 jsonObj1,但如果我尝试,我想发送两个参数:

context: {$implicit:jsonObj1, $implicit:jsonObj2}

并尝试使用

访问
<ng-template #filterTemplate let-Json1 let-json2>
</ng-template>

不行,告诉我,如何传递两个参数。

【问题讨论】:

    标签: html angular


    【解决方案1】:

    你不需要使用$implicit

    你可以使用

    1:

    context: {$implicit:jsonObj1, b:jsonObj2}
    

    <ng-template #filterTemplate let-json1 let-json2="b">
      <div>{{json1}}</div></div>{{json2}}</div>
    </ng-template>
    

    或 2:

    context: {$implicit: {a: jsonObj1, b:jsonObj2}}
    

    <ng-template #filterTemplate let-json1>
      <div>{{json1.a}}</div></div>{{json1.b}}</div>
    </ng-template>
    

    或 3:

    context: {a:jsonObj1, b:jsonObj2}
    

    <ng-template #filterTemplate let-json1="a" let-json2="b">
      <div>{{json1}}</div></div>{{json2}}</div>
    </ng-template>
    

    【讨论】:

    • 最佳答案。通常,人们会搜索第三种情况。
    【解决方案2】:

    这里有几个(类似的)选项,这个选项包括使用“ngTemplateOutletContext”和一个条件(在第四个参数中——为了好玩)。

    ...尝试 - 应该通过复制和粘贴来工作...

                <!-- DEMO using: 
                    "=templateID; context:{prop:value, ...}"
                    ( 4 arguments demo)
                    Note: $implicit identifies the default argument in the template.
                            The template does not need to assign the argument name,
                            - see the 3rd argument
                -->
        <div *ngFor="let item of ['Aaa', 'Bbb', 'Ccc']; index as ix">
            <ng-container *ngTemplateOutlet="myTemplate1; 
                        context:{cDemoName:'Option 1:',
                                 cIx:ix+1, 
                                 $implicit:item, 
                                 cIsEven: ((ix % 2) === 0) ? 'true' : 'false' }">
            </ng-container>
        </div>
    
        <hr>
    
                <!-- DEMO using: 
                    [ngTemplateOutlet]="templateID"
                    [ngTemplateOutletContext]="{"=templateID; context:{prop:value, ...}"
                -->
    
        <div *ngFor="let item of ['Dddd', 'Eee', 'Fff']; index as ix">
            <ng-container [ngTemplateOutlet]="myTemplate1"
                          [ngTemplateOutletContext]="{
                            cDemoName:'Option 2',
                            cIx:ix+1, 
                            $implicit:item, 
                            cIsEven: ((ix % 2) === 0) ? 'true' : 'false' }
                        ">
            </ng-container>
        </div>
    
                <!-- DEMO template: 
                    ( 4 arguments expected)
                -->
        <ng-template #myTemplate1 
                let-cDemoName="cDemoName"
                let-cIx="cIx"
                let-cItem
                let-cIsEven="cIsEven">
    
            Context arguments demo name: {{cDemoName}} <br>
            . . . . . Context index: {{cIx}} <br>
            . . . . . Context item: --- {{ cItem }} --- <br>
            . . . . . Context is-even: {{ cIsEven }} <br>
            <br>
        </ng-template>
    

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多