【问题标题】:Using Angular2 With Material Design Lite使用 Angular2 和 Material Design Lite
【发布时间】:2016-10-18 22:49:53
【问题描述】:

我正在学习 Angular2,但在将 Angular2 与 MDL 结合使用时遇到问题。 为什么 MDL 导航栏不适用于 Angular2?当我使用带有标题和抽屉的导航栏时,抽屉不工作,所以我无法点击它,我看不到抽屉的图标。 还有另一个问题:文本字段也无法正常工作。我想使用 mdl-textfield--expandable (搜索)但是当我点击这个搜索字段时它没有扩展。但是,如果没有 Angular2,它可以正常工作。

更新

这是我的 app.html 文件

<div class="demo-layout-waterfall mdl-layout mdl-js-layout">
    <header class="mdl-layout__header mdl-layout__header--waterfall">
        <!-- Top row, always visible -->
        <div class="mdl-layout__header-row">
            <!-- Title -->
            <span class="mdl-layout-title">Title</span>
            <div class="mdl-layout-spacer"></div>



            <div class="mdl-textfield mdl-js-textfield mdl-textfield--expandable
                  mdl-textfield--floating-label mdl-textfield--align-right">
                <label class="mdl-button mdl-js-button mdl-button--icon" for="waterfall-exp">
                    <i class="material-icons">search</i>
                </label>
                <div class="mdl-textfield__expandable-holder">
                    <input class="mdl-textfield__input" type="text" name="sample" id="waterfall-exp">
                </div>
            </div>


        </div>
        <!-- Bottom row, not visible on scroll -->
        <div class="mdl-layout__header-row">
            <div class="mdl-layout-spacer"></div>
            <!-- Navigation -->
            <nav class="mdl-navigation">
                <a class="mdl-navigation__link" href="">Link</a>
                <a class="mdl-navigation__link" href="">Link</a>
                <a class="mdl-navigation__link" href="">Link</a>
                <a class="mdl-navigation__link" href="">Link</a>
            </nav>
        </div>
    </header>

    <div class="mdl-layout__drawer">
        <span class="mdl-layout-title">Title</span>
        <nav class="mdl-navigation">
            <a class="mdl-navigation__link" href="">Link</a>
            <a class="mdl-navigation__link" href="">Link</a>
            <a class="mdl-navigation__link" href="">Link</a>
            <a class="mdl-navigation__link" href="">Link</a>
        </nav>
    </div>

    <main class="mdl-layout__content">
        <div class="page-content">
            <!-- Your content goes here -->
            
           

        </div>
    </main>
</div>

app.ts

import { Component } from 'angular2/core';


@Component({
    selector: 'my-app',
    templateUrl: 'app/app.html',
    styleUrls: ['app/assets/css/material.min.css']

})
export class AppComponent { }

main.ts

import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'

bootstrap(AppComponent);

【问题讨论】:

  • 欢迎来到 StackOverflow。请查看有关如何提出好问题的帮助菜单。请添加演示您尝试过的代码。解释实际和预期的行为是什么。 “不起作用”不包含任何有用的信息。
  • 我已经解释了什么不起作用。它只是没有显示抽屉!代码人人皆知,因为它在 (MDL website)
  • 请再次阅读我上面的评论并回复每句话。否则,您获得任何有用响应的机会约为 0。
  • @GünterZöchbauerm 我已经添加了,你会回答吗?
  • 我认为下面的答案是一个很好的猜测。

标签: angular material-design material-design-lite


【解决方案1】:

正如@talkdirty 解释的那样:

MDL 组件处理程序(您包含的处理动画和布局的 mdl 的 js 部分)对您包含在该模板中的 mdl 组件一无所知,因为它是在 MDL 组件处理程序之后的某个时间由 Angular2 动态包含的寻找要处理的 MDL 组件。

他的解决方案适用于单个组件:

declare var componentHandler: any;
export class MyComponent {
    ngAfterViewInit(){
        componentHandler.upgradeAllRegistered();
    }
}

但是当使用Angular2路由时,你需要在每个路由组件上添加这段代码,这不是很聪明和方便

作为一种解决方案,您可以使用AfterViewChecked 接口代替'AfterViewInit'

完整解决方案:
编写一个“mdl”指令,您将在应用组件的根 HTML 标记上使用该指令。

import {Directive, AfterViewChecked} from '@angular/core';
declare var componentHandler: any;

@Directive({
    selector: '[mdl]'
})
export class MDLUpgradeElementDirective implements AfterViewChecked {
    ngAfterViewChecked() {
        componentHandler.upgradeAllRegistered();
    }
}

然后声明你的根组件如下:

import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import { MDLUpgradeElementDirective } from './mdl-upgrade-element.directive';

@Component({
  moduleId: module.id,
  selector: 'app-root',
  template: '
  <div mdl class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
    <header class="mdl-layout__header">
      <div class="mdl-layout__header-row">
        <!-- Title -->
        <span class="mdl-layout-title">{{title}}</span>
      </div>
    </header>
    <main class="mdl-layout__content">
      <!-- Router Outlet -->
      <router-outlet></router-outlet>
    </main>
  </div>
  '
  directives: [
    ROUTER_DIRECTIVES,
    MDLUpgradeElementDirective
  ]
})
export class AppComponent {
  title = 'MDL Application';
}

那么您就不必再担心每个组件和路由上的 mdl js 处理程序了。

【讨论】:

  • 谢谢!甚至可以在 @angular@2.0.0-rc.5 上工作
  • 在 Angular 2 RTM 上使用 1.3.0 的材料 js。确保不要在脚本标签中使用“defer”,否则 componentHandler 将未定义。
  • @Julien Boulay 您的解决方案不再起作用我得到与post 相同的错误。更新解决方案会有帮助
【解决方案2】:

由于您没有发布任何代码示例,我猜您是在这种情况下(这是在 Angular2 中使用 MDL 时的常见陷阱):您在 Angular 2 中包含 MDL @Component模板。

@Component({
    template: `mdl things here`,
    ....
}) export class YourComponent {}

如果是这种情况,MDL 组件处理程序(您包含的处理动画和布局的 mdl 的 js 部分)对您包含在该模板中的 mdl 组件一无所知,因为它有时会被 Angular2 动态包含在 MDL 组件处理程序查找要处理的 MDL 组件之后。

长话短说,告诉 MDL 在组件初始化后重新检查 DOM 以获取要处理的新组件:

declare var componentHandler: any;
export class MyComponent {
    ngAfterViewInit(){
        componentHandler.upgradeAllRegistered();
    }
}

【讨论】:

  • 我已经添加了componentHandler。但是,它并没有解决我的问题
  • ngAfterViewInit() 只触发once,这意味着之后发生的模板更改不会被捕获。 ngAfterContentChecked(),如果改为使用,将在应用程序加载时工作,并将升级事后添加的任何组件。
  • 我知道这是旧的,但是 Angular 怎么知道在这种情况下 componentHandler 是什么?
猜你喜欢
  • 2016-05-22
  • 2017-06-18
  • 2017-01-21
  • 1970-01-01
  • 2016-03-29
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多