【问题标题】:Angular2: ng-switch not working as expectedAngular2:ng-switch 没有按预期工作
【发布时间】:2017-03-23 04:17:27
【问题描述】:

我按照官方网站的ng-switch语法:

https://angular.io/docs/ts/latest/api/common/index/NgSwitch-directive.html

这就是我使用 ng-switch 的方式:

<div ng-switch="accessLevel">
<div class="customer" ng-switch-when="ENABLED">This is customer data</div>
<div class="customer-blurr"  ng-switch-when="DISABLED"> This is disabled Customer Data</div>
<div class="customer-blurr" ng-switch-default> <demo-error [componentName]="componentname"></demo-error></div>
</div>

accessLevel 为“禁用”,但我仍然收到“这是客户 数据”。

我打印出 accessLevel 只是为了验证及其“DIABLED”

这是我的组件:

@Component({
  selector: 'customer',
  templateUrl: './customer.component.html',
  styleUrls: ['./customer.component.css']
})
export class CustomerComponent extends SuperChildComponent{
  public allowed: boolean = false;
  public  accessLevel:AccessLevel =null;
  public componentname:string;

  constructor(private authenticationService : AuthorizationService) {
    super();
    this.componentname=this.constructor.name;
     this.accessLevel=this.authenticationService.isUserLoggedIn()?this.authenticationService.componentAccessLevel(this.constructor.name):null;
     console.log(this.constructor.name +' has '+this.accessLevel);
  }

也试过了

ng-switch-when="accessLevel==ENABLED" 和 ng-switch-when="accessLevel===ENABLED"

但它仍然只执行第一个切换条件(这是我的想法),因为 accessLevel 是“禁用”

请帮帮我。

谢谢。

【问题讨论】:

    标签: angular


    【解决方案1】:

    您的代码看起来像 Angularjs 1.x 语法

    在 Angular2 中它是 *ngSwitchCase, *ngSwitchDefault, ...

    另见

    【讨论】:

    • 非常感谢:) 我忽略了文档中的角度版本
    猜你喜欢
    • 1970-01-01
    • 2016-09-05
    • 2014-10-14
    • 2017-08-20
    • 2018-04-24
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    相关资源
    最近更新 更多