Integrating Material Design Lite with Angular2

后端 未结 7 1494
再見小時候
再見小時候 2020-11-27 13:56

I have a small problem in integrating a meterial design (http://www.getmdl.io/) in ng2 Can you please help me I will put it in points what I have done

  1. http://w
7条回答
  •  感动是毒
    2020-11-27 14:14

    Thanks guys, works like a charm, to wrap this up a complete solution, that works well for me (tested with beta6). Without too much boilerplate code. The only thing I did not get to work are really dynamically added elements via *ngFor depending on a component variable. See dynamic elements in the template. Maybe one of you guys knows how to get around that.

    See a working plunkr (the preview must be at least 1024px wide to see the header)

    Install MDL

    npm i material-design-lite --save
    

    Reference it in your index.html

    
    
    
    

    Create MaterialDesignLiteUpgradeElement.ts

    import {Directive, AfterViewInit} from 'angular2/core';
    declare var componentHandler: any;
    
    @Directive({
        selector: '[mdl]'
    })    
    export class MDL implements AfterViewInit {
        ngAfterViewInit() {
            componentHandler.upgradeAllRegistered();
        }
    }
    

    Then in your component import and register it

    import { Component } from '@angular/core';    
    import { MDL } from '../../../directives/MaterialDesignLiteUpgradeElement';
    
    @Component ({
      selector: 'my-component',
      directives: [ MDL ],
      templateUrl: 'app/components/Header/Header.html'
    })
    export class Header {
      public dynamicArray:number[] = [];
    
      add() {
        this.dynamicArray.push(Math.round(Math.random() * 10));
      }
    }
    

    And in your template add mdl to the root component

    Home
    • Static entry
    • Semi Dynamic entry {{nr}}
    • Dynamic entry {{nr}}

提交回复
热议问题