gpt4 book ai didi

angular - @Output 从指令发出值到父组件 Angular 4

转载 作者:太空狗 更新时间:2023-10-29 18:06:42 25 4
gpt4 key购买 nike

我有一些上传指令,非常简单,唯一的问题是我必须从这个指令组件向父组件发送值。有人知道一个简单的解决方案吗?提前致谢。这是我现在的指令:

上传字段.component.ts

import { Component, OnInit, Input } from '@angular/core';
import { TooltipModule } from 'ngx-bootstrap/tooltip';

@Component({
selector: 'app-upload-field',
templateUrl: './upload-field.component.html',
styleUrls: ['./upload-field.component.scss']
})
export class UploadFieldComponent implements OnInit {

@Input() labelName: string;
@Input() placeHolderValue: string;
value = '';

constructor() { }

ngOnInit() {
}

uploadButton(event: any) {
this.value += event.target.value;
this.value = this.value.replace(/^.*\\/, '');
}

}

上传字段.component.html

<input placeholder="{{placeHolderValue}}" disabled="disabled" class="form-control first-input" value="{{value}}" />
<div class="file-upload">
<span class="btn btn-default btn-lg">{{labelName}}</span>
<input type="file" class="form-control upload-button" (change)="uploadButton($event)" value="{{value}}" />
</div>

我是这样使用它的:

<app-upload-field [labelName]="'Blader'" [placeHolderValue]="'Kies bestand'"></app-upload-field>

最佳答案

为此,您可以使用 EventEmitter

引用: Parent listens for child event

import { Component, Output, EventEmitter } from '@angular/core';
@Component({
//...your decorator properties
})
export class UploadFieldComponent {
@Output() onValueChanged = new EventEmitter<any>();

uploadButton() {
this.value += event.target.value;
this.value = this.value.replace(/^.*\\/, '');
this.onValueChanged.emit(this.value);
}

}

在父组件中,模板:

<app-upload-field [labelName]="'Blader'" [placeHolderValue]="'Kies bestand'" 
(onValueChanged)=onValueChanged($event)>
</app-upload-field>

在组件代码中,

 onValueChanged(value) {
// value will be the emitted value by the child
}

The child component exposes an EventEmitter property with which it emits events when something happens. The parent binds to that event property and reacts to those events.

关于angular - @Output 从指令发出值到父组件 Angular 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48556929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com