Angular2:父子组件通信

前端之家收集整理的这篇文章主要介绍了Angular2:父子组件通信前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个父子组件,其中子组件将有一个状态下拉.有人可以帮我理解如何访问父组件中的状态下拉值吗?这是我的示例代码.
/app/app.ts

import {Component} from 'angular2/core'
import {FORM_DIRECTIVES,FormBuilder,ControlGroup,Validators} from 'angular2/common'
import {State} from './state'

@Component({
  selector: 'my-app',providers: [FormBuilder],templateUrl: 'app/app.html',directives: [State]
})

export class App {
  registrationForm: ControlGroup;

  state: State;

  constructor(fb: FormBuilder) {

    this.registrationForm = fb.group({
      'name': ['',Validators.required],'email': ''
    });
  }

  onSubmit() {
    alert('Entered Name: ' + this.registrationForm.value.name);
    alert('State Selected: '); //trying to alert the state selected in state component
  }
}

/app/app.html

<div>
  <h2>Registration Form</h2>
  <form [ngFormModel]=registrationForm (ngSubmit)="onSubmit()">
    <label>Name: </label>
    <input type="text" ngControl="name">
    <state></state>
    <button [disabled]="!registrationForm.valid">Submit</button>
  </form>
</div>

/app/state.ts
import {Component} from 'angular2/core'
import {FORM_DIRECTIVES,ControlGroup} from 'angular2/common'

@Component({
  selector: 'state',templateUrl: 'app/state.html',directives: []
})

export class State {

  statesDropDownValues = ['NJ','NY','PA','CA'];

  stateForm: ControlGroup

  constructor(fb: FormBuilder) {
    this.stateForm = fb.group({
      'state': ''
    });

  }

  setStateValue() {
    alert('State Selected: ' + this.stateForm.value.state);
  }
}

/app/state.html
<div>
  <form [ngFormModel]="stateForm">
    <label>State: </label>
    <select ngControl="state" (change)="setStateValue()">
      <option *ngFor="#s of statesDropDownValues"
          [value]="s">{{s}}
      </option>
    </select>
  </form>
</div>

也是这里的龙头http://plnkr.co/edit/8tsm9sYeH8d8ulfqQKxY?p=info

我将输出定义到您的State组件并使用它触发事件:
@Component({
  selector: 'state',directives: []
})
export class State {
  statesDropDownValues = ['NJ','CA'];
  stateForm: ControlGroup;
  @Output()
  stateChange:EventEmitter<string> = new EventEmitter(); // <----

  constructor(fb: FormBuilder) {
    this.stateForm = fb.group({
      'state': ''
    });
  }

  setStateValue() {
    alert('State Selected: ' + this.stateForm.value.state);
    stateChange.emit(this.stateForm.value.state);
  }
}

父组件可以在此事件上注册通知更改:

<div>
  <h2>Registration Form</h2>
  <form [ngFormModel]=registrationForm (ngSubmit)="onSubmit()">
    <label>Name: </label>
    <input type="text" ngControl="name">
    <state (stateChange)="handleNewState($event)"></state>
    <button [disabled]="!registrationForm.valid">Submit</button>
  </form>
</div>

$event包含新状态值的值.

编辑

这是一种在父组件中保存选定状态的方法

export class App {
  registrationForm: ControlGroup;

  state: string;

  constructor(fb: FormBuilder) {
    this.registrationForm = fb.group({
      'name': ['','email': ''
    });
  }

  handleNewState(state) {
    this.state = state;
  }

  onSubmit() {
    alert('Entered Name: ' + this.registrationForm.value.name);
    alert('State Selected: ' + this.state);
  }
}
原文链接:https://www.f2er.com/angularjs/142454.html

猜你在找的Angularjs相关文章