Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angular2省市县三级联动,封装组件,使用@Output()传递给父组件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

再讲之前先讲一下父组件获取子组件的数据@Output()

A: 子组件需要做的工作

1. 引入Output和EventEmitter

import {Component,OnInit,Output,EventEmitter} from "@angular/core";

2. 定义输出方法provinceOut将在父组件中使用子组件标签的时候调用(后解)

@Output() provinceOut = new EventEmitter();

3. 单机模板中绑定的事件的时候发射变量到父组件 

provinceChange() {
    // 选择省份的时候发射省份给父组件
    this.provinceOut.emit(this.province);
}

B: 父组件要做的工作

1. 父组件模板中,调用子组件标签,绑定子组件中output的事件

<child-page (provinceOut)="recPro($event)"></child-page>
(provinceOut:子组件中@Output()定义的变量,recPro(#event)父组件中定义一个函数调用这个函数获取子组件传递过来的数据)

2. 父组件

// 函数接受子函数传递过来的变量
recPro(event) {
  this.province = event;
}

下面开始讲解三级联动组件,省市县数据使用Node(express)提供,使用jsonp跨域获取数据,后端代码就不多说了,简单的增删改查

1、子组件 three-link.component.ts

import {Component,EventEmitter} from "@angular/core";
import {URLSearchParams} from "@angular/http";
import {httpServer} from "../servers/http.server";
@Component({
  SELEctor: "three-link",templateUrl: "../templates/three-link.component.html"
})
export class ThreeLinkComponent implements OnInit {

  // 输出以下参数
  @Output() provinceOut = new EventEmitter();
  @Output() cityOut = new EventEmitter();
  @Output() countryOut = new EventEmitter();

  province:string;
  city:string;
  country:string;
  provinceData:Array<Object>;
  cityData:Array<Object>;
  countryData:Array<Object>;

  constructor(public httpServer:httpServer) {
    // 设置省市县的认值,显示请选择,要不然会有一个问题(选择省份的时候,市改变了,但是县没有变)
    this.province = "-1";
    this.city = "-1";
    this.country = "-1";
    this.provinceData = [];
    this.cityData = [];
    this.countryData = [];
  }

  // 选择省份
  ngOnInit() {
    // 初始化省市县
    var url = "http://localhost:3000/province";
    var params = new URLSearchParams();
    params.set("callBACk","JSONP_CALLBACK");

    this.httpServer.jsonpGet(url,params).subscribe(res=> {
      console.log(res);
      this.provinceData = res;
    });
    this.provinceChange();
    this.cityChange();
    this.countryChange();
  }

  // 选择省份,查询相应的provinceChange() {
    // 选择省份的时候发射省份给父组件
    this.provinceOut.emit(this.province);

    var url = "http://localhost:3000/city";
    var params = new URLSearchParams();
    params.set("ProID",this.province);
    params.set("callBACk","JSONP_CALLBACK");
    this.httpServer.jsonpGet(url,params).subscribe(res=> {
      console.log(res);
      this.cityData = res;
    });
    this.countryData = [];
  }

  // 选择市,查询相应的县
  cityChange() {
    // 选择市的时候发射市给父组件
    this.cityOut.emit(this.city);

    var url = "http://localhost:3000/country";
    var params = new URLSearchParams();
    params.set("CityID",this.city);
    params.set("callBACk",params).subscribe(res=> {
      console.log(res);
      this.countryData = res;
    });
  }

  // 选择市的时候发射县给父组件
  countryChange() {
    this.countryOut.emit(this.country);
  }
}

2、子模板 three-link.component.html

<p>
  <SELEct name="province" [(ngModel)]="province" class="form-control" (changE)="provinceChange()">
    <option value="-1">--请选择省份--</option>
    <option *ngFor="let item of provinceData" value="{{item.ProID}}">{{item.namE}}</option>
  </SELEct>
</p>
<p>
  <SELEct name="province" [(ngModel)]="city" class="form-control" (changE)="cityChange()">
    <option value="-1">--请选择市--</option>
    <option *ngFor="let item of cityData" value="{{item.CityID}}">{{item.namE}}</option>
  </SELEct>
</p>
<p>
  <SELEct name="province" [(ngModel)]="country" class="form-control" (changE)="countryChange()">
    <option value="-1">--请选择区县--</option>
    <option *ngFor="let item of countryData" value="{{item.ID}}">{{item.namE}}</option>
  </SELEct>
</p>

3、父模板

<!--三级联动组件-->
<three-link (provinceOut)="recPro($event)"
            (cityOut)="recCity($event)"
            (countryOut)="recCou($event)"
></three-link>

4、父组件

...
// 函数接受子函数传递过来的变量
  recPro(event) {
    this.province = event;
  }
  recCity(event) {
    this.city = event;
  }
  recCou(event) {
    this.country = event;
  }
...

5、获取数据的服务

import {InjectablE} from "@angular/core";
import {http,Jsonp} from "@angular/http";
import "rxjs/add/operator/map";
@Injectable()
export class httpServer {
  constructor(public http:http,public jsonp:Jsonp) {

  }

  httpGet(url,params) {
    return this.http.get(url,{search: params}).map(result=>result.json());
  }

  httpPost(url,params) {
    return this.http.post(url,{search: params}).map(result=>result.json());
  }

  jsonpGet(url,params) {
    return this.jsonp.get(url,{search: params}).map(result=>result.json());
  }
}

核心就是:定义省市县的组件,然后在需要用到的地方直接使用组件标签调用,但是父界面肯定需要获取子组件提供的省市县的值,所以要使用@Output()将子组件的值传递给父组件。

文件查看地址:https://github.com/zxc1989092...包含核心文件,和express后台路由文件

大佬总结

以上是大佬教程为你收集整理的angular2省市县三级联动,封装组件,使用@Output()传递给父组件全部内容,希望文章能够帮你解决angular2省市县三级联动,封装组件,使用@Output()传递给父组件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。