简体   繁体   English

Angular2 MDL禁用MDL菜单项不起作用

[英]Angular2 MDL disabling mdl-menu-item not working

I am trying to disable mdl-menu-item based on condition set by module. 我试图根据模块设置的条件禁用mdl-menu-item。

my app.component.ts 我的app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'ca-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  test() {
    return true;
  }
}

my app.component.html 我的app.component.html

<button mdl-button #btn1="mdlButton" (click)="m1.toggle($event, btn1)" mdl-button-type="raised" mdl-ripple>Options</button>
  <mdl-menu #m1="mdlMenu" mdl-menu-position="top-left">
    <mdl-menu-item mdl-ripple [disabled]="test()">Draw Object</mdl-menu-item>
    <mdl-menu-item mdl-ripple mdl-menu-item-full-bleed-divider>Another Action</mdl-menu-item>
    <mdl-menu-item mdl-ripple disabled>Disabled Action</mdl-menu-item>
    <mdl-menu-item>Yet Another Action</mdl-menu-item>
  </mdl-menu>

At this stage the menu item is never disabled, not sure what i am doing wrong here. 在此阶段,菜单项永远不会被禁用,不确定在这里我在做什么错。

The disabled attribute is a ui only feature in material design lite. 在Material Design Lite中,disabled属性是仅UI的功能。 eg there are only some css rules that change the ui if the disabled attribute is present on an mdl-menu-item. 例如,如果禁用属性出现在mdl-menu-item上,则只有一些CSS规则会更改ui。 So in your case you can do the following: 因此,您可以执行以下操作:

<mdl-menu-item [attr.disabled]="test() ? '' : null">Draw Object</mdl-menu-item>

The null value removes the attribute. 空值将删除该属性。 Also you should note that the click event is fired in any case. 另外您还应注意,在任何情况下都会触发click事件。

This could be improved but I think I would break existing behavior. 这可以改善,但我认为我会破坏现有行为。 I have filed an issue for the next major release to make it more angular like ( https://github.com/mseemann/angular2-mdl/issues/797 ). 我已经提交了下一个主要版本的问题,以使其更具棱角感,例如( https://github.com/mseemann/angular2-mdl/issues/797 )。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM