Angular文件输入更改仅在第一个组件中显示
创始人
2024-10-30 00:00:45
0

这可能是因为在第一个组件上绑定了同一个输入变量。在这种情况下,只有第一个组件能够获取新的输入值。可以使用一个服务来连接多个组件并共享输入值。示例如下:

在服务中创建一个Subject,在组件中使用该Subject来共享输入值:

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable({
    providedIn: 'root'
})
export class InputService {
    private inputSubject = new Subject();
    input$ = this.inputSubject.asObservable();

    setInput(input: any) {
        this.inputSubject.next(input);
    }
}

在第一个组件中,在输入值更改时使用setInput来更新服务中的输入值:

import { Component } from '@angular/core';
import { InputService } from './input.service';

@Component({
    selector: 'app-component-one',
    template: `
        
    `
})
export class ComponentOne {
    constructor(private inputService: InputService) {}

    updateInput(event) {
        const input = event.target.value;
        this.inputService.setInput(input);
    }
}

在第二个组件中,使用服务中的输入值:

import { Component } from '@angular/core';
import { InputService } from './input.service';

@Component({
    selector: 'app-component-two',
    template: `
        

{{ input }}

` }) export class ComponentTwo { input: any; constructor(private inputService: InputService) {} ngOnInit() { this.inputService.input$.subscribe(input => { this.input = input; }); } }

相关内容

热门资讯

第6分钟辅助!德扑HHpoke... 第6分钟辅助!德扑HHpoker有挂吗,hardrock透视工具,方案教程(了解有挂)1、下载好德扑...
第7分钟辅助!wepoker可... 第7分钟辅助!wepoker可以开透视吗,epoker免费透视脚本,指南教程(有挂教学)1、首先打开...
三分钟辅助!wepoker免费... 三分钟辅助!wepoker免费脚本咨询,aapoker如何设置胜率,手册教程(果真有挂)1、很好的工...
第7分钟辅助!wepoker私... 第7分钟辅助!wepoker私人局辅助,hhpoker是真的还是假的,法子教程(有挂透明挂)hhpo...
四分钟辅助!wepoker分析... 四分钟辅助!wepoker分析,werplan怎么透视,模板教程(有挂方式)该软件可以轻松地帮助玩家...
第6分钟辅助!哈糖大菠萝软件下... 第6分钟辅助!哈糖大菠萝软件下载,aapoker怎么开辅助器,法子教程(证实有挂)1、哈糖大菠萝软件...
七分钟辅助!hhpoker透视... 七分钟辅助!hhpoker透视方法,we poker辅助器下载,机巧教程(有挂存在)1、we pok...
三分钟辅助!werplan免费... 三分钟辅助!werplan免费挂下载,拱趴大菠萝挂怎么安装,项目教程(有挂方针)1、完成拱趴大菠萝挂...
第9分钟辅助!wpk透视辅助靠... 第9分钟辅助!wpk透视辅助靠谱吗,wepoker透视脚本视频,机巧教程(有挂教学)wepoker透...
第十分钟辅助!werplan辅... 您好,wepoker游戏安装教程这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...