Angular 2 / Material 2无法实现select组件

时间:2016-12-09 13:48:10

标签: angular angular2-forms angular-material2

我正在尝试从Material 2 Demo应用程序中给出的示例中实现选择下拉菜单,我收到以下错误。任何帮助都会很棒。

enter image description here

组件:

import { Component } from '@angular/core';
import { NgForm, FormControl } from '@angular/forms';

import { Tenant } from '../../shared/models/tenant.model';

import { InviteTenantHttp } from './invite-tenant-http.service';

@Component({
  selector: 'invite-tenant',
  templateUrl: './invite-tenant.component.html',
  styleUrls: ['./invite-tenant.component.scss']
})

export class InviteTenantComponent {
  model = new Tenant('', null, '');

  constructor(
    private inviteTenantHttp: InviteTenantHttp
  ) {}

  tenantGroups = [
    { value: 'group1', viewValue: 'group-1' },
    { value: 'group2', viewValue: 'group-2' },
    { value: 'group3', viewValue: 'group-3' }
  ];

  submit(form: NgForm) {
    this.inviteTenantHttp.post(this.model)
      .subscribe(
        data => {
          console.log(data);
        },
        error => console.log(error)
      );
  }
}

组件模板:

<div class="invite-tenant-container">
  <header class="invite-tenant-header">
    <div class="invite-tenant-site-info"></div>
    <div class="invite-tenant-cyber-score"></div>
  </header>
  <section class="invite-tenant-content">
    <h3>Invite Tenant to //Site Name\\</h3>
    <form #form="ngForm" (submit)="submit(form)" novalidate>
            <md-input
                type="email"
                name="email"
                [(ngModel)]="model.username"
                placeholder="Email"
                required>
            </md-input>
            <md-input
                type="password"
                name="password"
                [(ngModel)]="model.password"
                placeholder="Password"
                required>
            </md-input>
            <md-select
                placeholder="Tenant Group"
                [(ngModel)]="model.tenantGroup"
                #tenantControl="ngModel">
                <md-option *ngFor="let group of tenantGroups" [value]="group.value">
                    {{ group.viewValue }}
                </md-option>
            </md-select>
            <button type="submit">submit</button>
        </form>
  </section>
</div>

型号:

export class Tenant {
  constructor(
    public username: string,
    public password: number,
    public tenantGroup: string
  ) {}
}

以下是演示应用实施的组件和模板:

组件:

  <md-card>
    <md-select placeholder="Drink" [(ngModel)]="currentDrink" [required]="isRequired" [disabled]="isDisabled"
      #drinkControl="ngModel">
      <md-option *ngFor="let drink of drinks" [value]="drink.value" [disabled]="drink.disabled">
        {{ drink.viewValue }}
      </md-option>
    </md-select>
    <p> Value: {{ currentDrink }} </p>
    <p> Touched: {{ drinkControl.touched }} </p>
    <p> Dirty: {{ drinkControl.dirty }} </p>
    <p> Status: {{ drinkControl.control?.status }} </p>
    <button md-button (click)="currentDrink='sprite-1'">SET VALUE</button>
    <button md-button (click)="isRequired=!isRequired">TOGGLE REQUIRED</button>
    <button md-button (click)="isDisabled=!isDisabled">TOGGLE DISABLED</button>
  </md-card>

</div>

组件:

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

@Component({
    moduleId: module.id,
    selector: 'select-demo',
    templateUrl: 'select-demo.html',
    styleUrls: ['select-demo.css'],
})
export class SelectDemo {
  isRequired = false;
  isDisabled = false;
  currentDrink: string;
  foodControl = new FormControl('');

  foods = [
    {value: 'steak-0', viewValue: 'Steak'},
    {value: 'pizza-1', viewValue: 'Pizza'},
    {value: 'tacos-2', viewValue: 'Tacos'}
  ];

  drinks = [
    {value: 'coke-0', viewValue: 'Coke'},
    {value: 'sprite-1', viewValue: 'Sprite', disabled: true},
    {value: 'water-2', viewValue: 'Water'}
  ];

  toggleDisabled() {
    this.foodControl.enabled ? this.foodControl.disable() : this.foodControl.enable();
  }

}

更新了工作演示图片:

enter image description here

1 个答案:

答案 0 :(得分:1)

正如Camden_kid所说,目前Angular Material 2中没有md-select

你可以链接你正在谈论的演示应用吗?以下是我找到的https://material2-app.firebaseapp.com/,链接自https://github.com/angular/material2 入门

据我所知,您正在尝试在角度2代码中使用角度1材质组件。

修改

在查看链接的演示应用程序后,对于此演示,它们会添加一些尚未发布的组件。这可能是您无法让md-select在您的项目上工作的原因:除非您手动添加相关文件,否则您没有他们在演示中使用的来源。

如果您需要,相关文件位于demo-app repo内的dist/@angular/material/select。您应该可以通过在项目的md-select文件夹中复制粘贴此文件夹来使用nodes_modules/@angular/material

然而 我强烈反对这个选项,因为如果它尚未发布,则意味着它可能会在最终版本发布之前发生深刻变化,这意味着需要进行必要的重构。你这个时候。