Angular5 - Change parent#39;s property from child component#39;s input#39;s [(ngModel)] binding(Angular5 - 从子组件的输入的 [(ngModel)] 绑定更改父的属性)
问题描述
我已经看过 这个 类似的问题没有成功.问题中提到的plunker似乎坏了.
I have already looked at this similar question without success. The plunker mentioned in the question seems to be broken.
我正在尝试从子组件的 [(ngModel)] 绑定更新父组件的属性.
I am trying to update parent component's property from child component's [(ngModel)] binding.
这是子组件 HTML:
This is the child components HTML:
<div class="elastic-textarea">
<ion-input rows="1" [value]="inputValue" [(ngModel)]="inputValue" (ngModelChange)="change($event)" ></ion-input>
</div>
这是子组件TS:
import { Component, EventEmitter, Input, Output, OnInit } from '@angular/core';
@Component({
selector: 'app-childinput',
templateUrl: './childinput.component.html',
styleUrls: ['./childinput.component.css']
})
export class ChildinputComponent {
@Input() inputValue: string;
@Output() emitInputValue = new EventEmitter();
constructor() { }
change(newValue) {
console.log('newvalue', newValue)
this.inputValue = newValue;
this.emitInputValue.emit(newValue);
}
}
这是我在父组件中使用子组件的方式:
This is how I'm using the child component in the parent component:
<app-childinput [(inputValue)]="thevalue" ></app-childinput>
<p>The changed value should be reflected here: {{thevalue}}</p>
这是 STACKBLITZ 证明问题.父组件是名为home"的页面,子组件是名为childinput"的组件.
Here is a STACKBLITZ demonstrating the issue. The parent component is the page callled "home", and the child component is the component called "childinput."
是我做错了什么还是在 Angular 中这根本不可能?
Am I doing something wrong or is this simply not possible anymore in Angular?
推荐答案
只需将emitInputValue
改为inputValueChange
即可.
固定 Stackblitz
这篇关于Angular5 - 从子组件的输入的 [(ngModel)] 绑定更改父的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Angular5 - 从子组件的输入的 [(ngModel)] 绑定更改父的属性


基础教程推荐
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01