简体   繁体   English

Angular ngIf无法访问父属性

[英]Angular ngIf can't access parent property

I'm using Angular 5 and Material 5: 我正在使用Angular 5和Material 5:

<mat-tab-group>
    <mat-tab label="INFO">
        info goes here
    </mat-tab>
    <mat-tab label="CONTACTS">
        <div *ngIf="isActive">
            contacts should load here
        </div>
    </mat-tab>
</mat-tab-group>

I'm trying to show contacts, but only if tab is active. 我正在尝试显示联系人,但仅在选项卡处于活动状态时才显示。 There is isActive property for mat-tab ( https://github.com/angular/material2/blob/5.2.x/src/lib/tabs/tab.ts#L83 ), but I can't access it, it looks undefined. mat-tab有一个isActive属性( https://github.com/angular/material2/blob/5.2.x/src/lib/tabs/tab.ts#L83 ),但我无法访问它,它看起来未定义。

I can't understand what am I doing wrong. 我不明白我在做什么错。

(I also can't use newer version of Angular at the moment). (目前我也不能使用较新版本的Angular)。

This way it works: 这样工作:

<mat-tab-group>
    <mat-tab label="INFO">
        info goes here
    </mat-tab>
    <mat-tab #contactsTab label="CONTACTS">
        <div *ngIf="contactsTab.isActive">
            contacts should load here
        </div>
    </mat-tab>
</mat-tab-group>

Elements can't access the property of any element without a reference to it ( Docs: Components Interactions ) the div isn't treated as a part of mat-tab unless it's a part of it's template, even if it's inside it. 如果没有对元素的引用( Docs:Components Interactions ),元素就无法访问任何元素的属性,除非div是模板的一部分,即使它在模板内部,也不会被视为mat-tab的一部分。

I do not think you need the *ngIf . 我认为您不需要*ngIf If you inspect the tab body you should not see content in the inactive tabs. 如果检查选项卡正文,则应该在非活动选项卡中看不到内容。

For example, from this simple StackBlitz , there are two tabs and the second <mat-tab-body> has nothing inside. 例如,从这个简单的StackBlitz中 ,有两个选项卡,第二个<mat-tab-body>里面什么也没有。

Then, click the other tab and the reverse happens. 然后,单击另一个选项卡,相反的情况发生。

Source 资源

<mat-tab-group>
  <mat-tab label="One">
    <h1>Some tab content</h1>
    <p>...One</p>
  </mat-tab>
  <mat-tab label="Two">
    <h1>Some more tab content</h1>
    <p>...Two</p>
  </mat-tab>
</mat-tab-group>

DOM inspection, tab 1 is active DOM检查,选项卡1处于活动状态

<div class="mat-tab-body-wrapper">
  <!--bindings={
    "ng-reflect-ng-for-of": "[object Object],[object Object"
    }-->

  <mat-tab-body class="mat-tab-body ng-tns-c5-0 mat-tab-body-active ng-star-inserted" role="tabpanel" ng-reflect-_content="[object Object]" ng-reflect-position="0" id="mat-tab-content-0-0" aria-labelledby="mat-tab-label-0-0" ng-reflect-origin="0">
    <div class="mat-tab-body-content ng-trigger ng-trigger-translateTab" style="transform: none;">
      <!---->
      <h1 class="ng-star-inserted" style="">Some tab content</h1>
      <p class="ng-star-inserted" style="">...One</p>
      <!---->
    </div>
  </mat-tab-body>

  <mat-tab-body class="mat-tab-body ng-tns-c5-1 ng-star-inserted" role="tabpanel" ng-reflect-_content="[object Object]" ng-reflect-position="1" id="mat-tab-content-0-1" aria-labelledby="mat-tab-label-0-1">
    <div class="mat-tab-body-content ng-trigger ng-trigger-translateTab" style="transform: translate3d(100%, 0px, 0px);">
      <!---->
    </div>
  </mat-tab-body>

</div>

暂无
暂无

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

相关问题 Angular 2无法绑定到'ngif',因为它不是已知属性 - Angular 2 Can't bind to 'ngif' since it isn't a known property Angular 6 无法绑定到“*ngIf”,因为它不是已知属性 - Angular 6 Can't bind to '*ngIf' since it isn't a known property 从Angular的父组件访问子ngIf - Access child ngIf from parent component in Angular Angular - NgIf 无法在组件上工作:“NG0303:无法绑定到‘ngIf’,因为它不是‘p’的已知属性。” - Angular - NgIf not working on component: "NG0303: Can't bind to 'ngIf' since it isn't a known property of 'p'." 无法读取未定义的 angular 6 *ngIf 问题的属性 - can not read property of undefined angular 6 *ngIf issue Angular Karma Jasmine 测试“无法绑定到 &#39;ngIf&#39;,因为它不是已知属性” - Angular Karma Jasmine test “Can't bind to 'ngIf' since it isn't a known property” 在控制台中出现错误无法绑定到“ngif”,因为它不是 angular 应用程序中“div”的已知属性 - Getting error in console Can't bind to 'ngif' since it isn't a known property of 'div' in angular app NX / Angular IntelliSense 的 VSCode IntelliSense 错误“无法绑定到 'ngIf',因为它不是 'div' 的已知属性” - VSCode IntelliSense error with NX / Angular IntelliSense "Can't bind to 'ngIf' since it isn't a known property of 'div'" angular 2 控制台错误无法绑定到“ngIf”,因为它不是“span”的已知属性 - angular 2 console error Can't bind to 'ngIf' since it isn't a known property of 'span' Angular 抛出错误:无法绑定到“ngIf”,因为它不是“ng-container”的已知属性 - Angular throws error: can't bind to “ngIf” since it isn't a known property of “ng-container”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM