Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. export interface IState {
  2. state: string;
  3. }
  4.  
  5. export interface ICity {
  6. state: string;
  7. city: string;
  8. }
  9.  
  10. import { Injectable } from '@angular/core';
  11. import { Http, Response } from '@angular/http';
  12. import { Observable } from 'rxjs/Observable';
  13. import 'rxjs/add/operator/map';
  14. import 'rxjs/add/operator/distinct';
  15. import 'rxjs/add/operator/catch';
  16. import { ICity } from './city.interface'
  17.  
  18. @Injectable()
  19. export class CityService {
  20. private _urlCity = '../../api/city.json';
  21.  
  22. constructor(private _http: Http) { }
  23.  
  24. getCity(stateName:string): Observable<ICity[]> {
  25. return this._http.get(this._urlCity)
  26. .map((response: Response) => <ICity[]>response.json())
  27. .catch(this.handleError);
  28. }
  29.  
  30. private handleError(error: Response) {
  31. console.error('I found something bad');
  32. console.error(error);
  33. return Observable.throw(error.json().error || 'Server error ...');
  34. }
  35.  
  36.  
  37. }
  38.  
  39. <div class="card-container">
  40. <md-card>
  41. <md-card-title>
  42. <h3>Testing Cascade Material Design</h3>
  43. </md-card-title>
  44. <md-card-content>
  45. <div *ngIf='allStates'>
  46. <form novalidate [formGroup]="myForm">
  47. <div class="flex-container" fxLayout="row" fxLayoutAlign="left left">
  48. <div class="flex-container" fxLayout="row" fxLayoutGap="20px" fxLayoutAlign="space-around space-around" fxFlex="97%">
  49. <div class="flex-oneStudent" fxFlex="10%">
  50. <md-select placeholder="State" formControlName="state" required="true" (change)="onSelect($event.target.value)">
  51. <md-option *ngFor="let oneState of allStates" [value]="oneState.state">
  52. {{ oneState.state }}
  53. </md-option>
  54. </md-select>
  55. </div>
  56. <div class="flex-oneStudent" fxFlex="20%">
  57. <md-select placeholder="City" formControlName="city" required="true">
  58. <md-option *ngFor="let oneCity of cityByState" [value]="oneCity.city">
  59. {{ oneCity.city }}
  60. </md-option>
  61. </md-select>
  62. </div>
  63. <div fxFlex="67%"> </div>
  64. </div>
  65. </div>
  66. </form>
  67. </div>
  68. </md-card-content>
  69. </md-card>
  70.  
  71. import { Component, OnInit } from '@angular/core';
  72. import { AbstractControl, FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
  73.  
  74. import { StateService } from '../State/state.service';
  75. import { CityService } from '../City/city.service';
  76. import { IState } from '../State/state.interface';
  77. import { ICity } from '../City/city.interface';
  78.  
  79. import * as _ from 'lodash';
  80.  
  81. @Component({
  82. selector: 'app-main',
  83. templateUrl: './main.component.html',
  84. styleUrls: ['./main.component.css']
  85. })
  86. export class MainComponent implements OnInit {
  87. myForm: FormGroup;
  88. allStates: IState[];
  89. cityByState: ICity[];
  90.  
  91. constructor(public fb: FormBuilder,
  92. private _StateService: StateService,
  93. private _CityService: CityService
  94. ) { }
  95.  
  96. ngOnInit() {
  97. this.myForm = this.fb.group({
  98. state: '',
  99. city: ''
  100. });
  101.  
  102. this._StateService.getState()
  103. .subscribe(
  104. stateData => this.allStates = _.uniqBy(stateData, 'state')
  105. );
  106. }
  107.  
  108. onSelect(stateName) {
  109. console.log ('User selected ' + stateName);
  110. this._CityService.getCity(stateName)
  111. .subscribe(
  112. cityData => this.cityByState = _.filter(cityData, function(o) { return o.state == stateName})
  113. );
  114.  
  115. }
  116.  
  117. }
  118.  
  119. @Output() change: EventEmitter<MdSelectChange> = new EventEmitter<MdSelectChange>();
  120.  
  121. export class MdSelectChange {
  122. constructor(public source: MdSelect, public value: any) { }
  123. }
  124.  
  125. (change)="onSelect($event.target.value)"
  126.  
  127. (change)="onSelect($event.value)"
  128.  
  129. (selectionChange)="onSelect($event.value)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement