如何更改角度8中子组件的下拉值

古腾

试图从角度8的子组件中更改下拉值,但我不知道该怎么做。如果有人知道,请帮助找到解决方案。

If i click France button want to set dropdown value France.
If i click Germany button want to set dropdown value Germany.
If i click Italy button want to set dropdown value Italy.

app.component:

<select name="city" class="rounded-inputs20 select-select col-md-3" (change)="onChange($event.target.value)">
  <option *ngFor="let country of countries"  [value]="country.id">{{country.name}}</option>
</select>

body.component:

<button (click)="changeVal('France')">France</button>

<button (click)="changeVal('Germany')">Germany</button>

<button (click)="changeVal('Italy')">Italy</button>

演示::https://stackblitz.com/edit/angular-dropdown-geishz?file=app%2Fapp.component.html

马尼拉吉·穆鲁甘(Maniraj Murugan)

您需要在此处实现事件绑定,以将数据从body组件传递app组件,并且需要使用Output()EventEmitter()

修改后的文件:

body.component.ts:

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

@Component({
  selector: 'app-body',
  templateUrl: './body.component.html',
  styleUrls: ['./body.component.css']
})
export class BodyComponent implements OnInit {

  @Output() valueChange = new EventEmitter()

  constructor() { }

  ngOnInit() {
  }

  changeVal(val){
    this.valueChange.emit(val);
  }

}

包括以下内容[(ngModel)]='selectedCountry'以跟踪应用程序组件html中的下拉更改,

app.component.html:

<p>
    App Component:
</p>

<select name="selectedCountry"  [(ngModel)]='selectedCountry' class="rounded-inputs20 select-select col-md-3" (change)="onChange($event.target.value)">
  <option *ngFor="let country of countries; let i = index" [value]="country.id"> 
    {{country.name}}</option>
</select>


<app-body (valueChange)="getValueChange($event)"></app-body>

在上面的代码中,(valueChange)="getValueChange($event)"是您从单击的相应按钮中接收数据的地方。

然后在应用程序组件ts文件中,您可以包含一个函数来处理主体组件中发生的按钮单击更改,并通过设置的值来更改下拉选择this.selectedCountry = selectedOption.id

  getValueChange(val) {
    const selectedOption = this.countries.find(x => x.name == val);
    this.selectedCountry = selectedOption.id;
    this.onChange(selectedOption);
  }

app.component.ts:

import { Component, OnInit  } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  name = 'Angular 5';

  cities = {};

  countries = [{
    id: 1, name: 'France' 
  },
  {
    id: 2, name: 'Germany' 
  },
  {
    id: 3, name: 'Italy' 
  },
  ];

  selectedCountry: any = this.countries[0].id;

  ngOnInit() {
    this.cities = this.countries.filter(x => x.id == 1);
  }

  onChange(deviceValue) {
    this.cities = this.countries.filter(x => x.id == deviceValue);
    console.log('onchange ', deviceValue)
  }

  getValueChange(val) {
    const selectedOption = this.countries.find(x => x.name == val);
    this.selectedCountry = selectedOption.id;
    this.onChange(selectedOption);
  }

}

分叉的Stackblitz:

https://stackblitz.com/edit/angular-dropdown-65tfxg

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

动态渲染时更改角度2中子组件的顺序

来自分类Dev

如何在子组件的子组件中更改 this.state 的属性值

来自分类Dev

如何在角度8的表td中显示多选下拉列表值

来自分类Dev

角度:@Input更改未在子组件中触发

来自分类Dev

如何更新子组件中的更改

来自分类Dev

如何在下拉菜单更改时重新加载角度指令/组件?

来自分类Dev

如何使在子组件中修改的值对父组件可用?

来自分类Dev

更改子组件中属性的值不起作用

来自分类Dev

更改根组件中的值时,不会更新子组件中的Vue.js props值

来自分类Dev

如何从类组件(父组件)更改功能组件(子组件)中的useState

来自分类Dev

如何更改jQuery的下拉值

来自分类Dev

如何使用jQuery更改下拉列表中的特定值

来自分类Dev

如何动态设置角度下拉值?

来自分类Dev

如何在角度 6 中获取对象子值

来自分类Dev

如何更改更改事件下拉列表的值?

来自分类Dev

如何从Reactjs的子组件中获取多个值?

来自分类Dev

如何以编程方式更改已经渲染的组件的输入值(角度2)

来自分类Dev

如何以编程方式更改已经渲染的组件的输入值(角度2)

来自分类Dev

如何在带有文字和角度的动态下拉列表中创建“子菜单”?

来自分类Dev

当RN 0.61中的状态更改时,如何从主组件中重新渲染子组件?

来自分类Dev

如何更改功能组件中的上下文值?

来自分类Dev

如何销毁动态角度8创建的组件

来自分类Dev

使用角度,如何在下拉列表中的ng-change中获取选定的值?

来自分类Dev

如何在角度8中显示转换后的值?

来自分类Dev

如何摆脱角度8中的不确定值?

来自分类Dev

角度-子组件在数据更改后被销毁,为什么?

来自分类Dev

角度-子组件在数据更改后被销毁,为什么?

来自分类Dev

从角度控制器更改下拉选择的值

来自分类Dev

基于下拉值的角度相对形式需要更改

Related 相关文章

  1. 1

    动态渲染时更改角度2中子组件的顺序

  2. 2

    如何在子组件的子组件中更改 this.state 的属性值

  3. 3

    如何在角度8的表td中显示多选下拉列表值

  4. 4

    角度:@Input更改未在子组件中触发

  5. 5

    如何更新子组件中的更改

  6. 6

    如何在下拉菜单更改时重新加载角度指令/组件?

  7. 7

    如何使在子组件中修改的值对父组件可用?

  8. 8

    更改子组件中属性的值不起作用

  9. 9

    更改根组件中的值时,不会更新子组件中的Vue.js props值

  10. 10

    如何从类组件(父组件)更改功能组件(子组件)中的useState

  11. 11

    如何更改jQuery的下拉值

  12. 12

    如何使用jQuery更改下拉列表中的特定值

  13. 13

    如何动态设置角度下拉值?

  14. 14

    如何在角度 6 中获取对象子值

  15. 15

    如何更改更改事件下拉列表的值?

  16. 16

    如何从Reactjs的子组件中获取多个值?

  17. 17

    如何以编程方式更改已经渲染的组件的输入值(角度2)

  18. 18

    如何以编程方式更改已经渲染的组件的输入值(角度2)

  19. 19

    如何在带有文字和角度的动态下拉列表中创建“子菜单”?

  20. 20

    当RN 0.61中的状态更改时,如何从主组件中重新渲染子组件?

  21. 21

    如何更改功能组件中的上下文值?

  22. 22

    如何销毁动态角度8创建的组件

  23. 23

    使用角度,如何在下拉列表中的ng-change中获取选定的值?

  24. 24

    如何在角度8中显示转换后的值?

  25. 25

    如何摆脱角度8中的不确定值?

  26. 26

    角度-子组件在数据更改后被销毁,为什么?

  27. 27

    角度-子组件在数据更改后被销毁,为什么?

  28. 28

    从角度控制器更改下拉选择的值

  29. 29

    基于下拉值的角度相对形式需要更改

热门标签

归档