具有材料设计和ReactiveForms的Angular2级联下拉列表

问题描述 投票:1回答:1

我正在使用Material Design和ReactiveForms处理Cascading Dropdown。代码有一个下拉状态,一旦选择将过滤城市下拉列表。

我发现这个例子http://www.talkingdotnet.com/cascading-dropdown-select-list-using-angular-js-2/但它没有使用reactiveForms。

目前屏幕加载没有错误。州的下拉列表有一个州列表。当选择状态时会发生这种情况... ./MainComponent类中的错误MainComponent - 内联模板:11:16由以下原因引起:无法读取未定义的属性“value”

这是州的界面

export interface IState {
    state: string;
}

这是城市的界面

    export interface ICity {
    state: string;
    city: string;
}

这是城市的服务

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/distinct';
import 'rxjs/add/operator/catch';
import { ICity } from './city.interface'

@Injectable()
export class CityService {
  private _urlCity = '../../api/city.json';

  constructor(private _http: Http) { }

  getCity(stateName:string): Observable<ICity[]> {
    return this._http.get(this._urlCity)
      .map((response: Response) => <ICity[]>response.json())
      .catch(this.handleError);
  }

  private handleError(error: Response) {
    console.error('I found something bad');
    console.error(error);
    return Observable.throw(error.json().error || 'Server error ...');
  }


}

这是主要组件的HTML

<div class="card-container">
  <md-card>
    <md-card-title>
      <h3>Testing Cascade Material Design</h3>
    </md-card-title>
    <md-card-content>
      <div *ngIf='allStates'>
        <form novalidate [formGroup]="myForm">
          <div class="flex-container" fxLayout="row" fxLayoutAlign="left left">
            <div class="flex-container" fxLayout="row" fxLayoutGap="20px" fxLayoutAlign="space-around space-around" fxFlex="97%">
              <div class="flex-oneStudent" fxFlex="10%">
                <md-select placeholder="State" formControlName="state" required="true" (change)="onSelect($event.target.value)">
                  <md-option *ngFor="let oneState of allStates" [value]="oneState.state">
                    {{ oneState.state }}
                  </md-option>
                </md-select>
              </div>
              <div class="flex-oneStudent" fxFlex="20%">
                <md-select placeholder="City" formControlName="city" required="true">
                  <md-option *ngFor="let oneCity of cityByState" [value]="oneCity.city">
                    {{ oneCity.city }}
                  </md-option>
                </md-select>
              </div>
              <div fxFlex="67%"> </div>
            </div>
          </div>
        </form>
      </div>
    </md-card-content>
  </md-card>

这是主要组成部分

import { Component, OnInit } from '@angular/core';
import { AbstractControl, FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';

import { StateService } from '../State/state.service';
import { CityService } from '../City/city.service';
import { IState } from '../State/state.interface';
import { ICity } from '../City/city.interface';

import * as _ from 'lodash';

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.css']
})
export class MainComponent implements OnInit {
  myForm: FormGroup;
  allStates: IState[];
  cityByState: ICity[];

  constructor(public fb: FormBuilder,
    private _StateService: StateService,
    private _CityService: CityService
  ) { }

  ngOnInit() {
    this.myForm = this.fb.group({
      state: '',
      city: ''
    });

    this._StateService.getState()
      .subscribe(
      stateData => this.allStates = _.uniqBy(stateData, 'state')
      );
  }

  onSelect(stateName) {
    console.log ('User selected ' + stateName);
    this._CityService.getCity(stateName)
      .subscribe(
      cityData => this.cityByState = _.filter(cityData, function(o) { return o.state == stateName})
      );

  }

}

这是完整代码的github。 https://github.com/ATXGearHead12/cascade

angular material-design lodash cascadingdropdown onselect
1个回答
3
投票

你需要看一下材料源代码https://github.com/angular/material2/blob/master/src/lib/select/select.ts#L247

@Output() change: EventEmitter<MdSelectChange> = new EventEmitter<MdSelectChange>();

正如您所看到的那样,MdSelectChange有效载荷会发出事件

export class MdSelectChange {
  constructor(public source: MdSelect, public value: any) { }
}

所以更换

(change)="onSelect($event.target.value)"

(change)="onSelect($event.value)"

*因为Angular 6.(更改)已弃用(selectionChange) qazxsw poi

see material 2 Breaking Changes > deprecations
© www.soinside.com 2019 - 2024. All rights reserved.