【问题标题】:In Angular, is there a way to change a certain variable's value when the page loads?在 Angular 中,有没有办法在页面加载时更改某个变量的值?
【发布时间】:2019-01-08 06:27:00
【问题描述】:

我有一个带有 3 个锚标记的导航栏。单击锚标记时,与其关联的某些组件是可见的;而与其他锚标记关联的组件被隐藏。 (即一次只有一个处于活动状态)我的问题是组件仅在您单击导航栏中的项目时才会显示。 ((点击)="...") 有没有办法在页面加载时初始化 a='Y' ? (不使用 JavaScript)

  <nav>
        <ul>
          <li><a href="#" (click)="a='Y'; b='N'; c='N'">Title a</a></li>
          <li><a href="#" (click)="a='N'; b='Y'; c='N'">Title b</a></li>
          <li><a href="#" (click)="a='N'; b='N'; c='Y'">Title c</a></li>
        </ul>
      </nav>

<ng-container *ngIf = "a==='Y'"> {some of a's components here....} </ng-container>

<ng-container *ngIf = "b==='Y'"> {some of b's components here....} </ng-container>

<ng-container *ngIf = "c==='Y'"> {some of c's components here....} </ng-container>

【问题讨论】:

标签: html angular angular5 frontend


【解决方案1】:

你在这里做的事情违背了 Angular 的最佳实践。

查看角度路由:https://angular.io/tutorial/toh-pt5

另外,为什么不在控制器中放入任何初始化代码呢?

【讨论】:

    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2012-08-01
    • 2020-06-09
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多