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; }); } }

相关内容

热门资讯

实测辅助!新超凡辅助,wepo... 实测辅助!新超凡辅助,wepokerplus辅助(透视辅助开挂辅助安装)1、下载安装好wepoker...
曝光辅助!雀友会潮汕麻将开挂辅... 曝光辅助!雀友会潮汕麻将开挂辅助,川南九九辅助(热点推荐开挂辅助工具)>>您好:软件加薇136704...
原来有辅助!新广西老友辅助,破... >>您好:破解辅助插件wepoker确实是有挂的,很多玩家在这款破解辅助插件wepoker游戏中打牌...
推荐开挂!西兵互娱辅助多少钱,... 推荐开挂!西兵互娱辅助多少钱,上饶辅助(玩家必看科普开挂辅助下载)西兵互娱辅助多少钱ai黑科技系统规...
推荐辅助!新道游正版开挂,打大... 新道游正版开挂 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、...
盘点开挂!微信小程序雀神挂件下... 盘点开挂!微信小程序雀神挂件下载,wepoker插件辅助(三分钟了解开挂辅助工具) 【无需打开直接搜...
正品辅助!天道联盟辅助软件,好... 正品辅助!天道联盟辅助软件,好友赣南脚本插件(透视有挂开挂辅助下载);无需打开直接搜索微信(1367...
关于辅助!财神13张有没有辅助... 关于辅助!财神13张有没有辅助工具,雀友会广东潮汕辅助(发现一款开挂辅助神器)您好:雀友会广东潮汕辅...
必看辅助!久久互娱科技,胡乐麻... 必看辅助!久久互娱科技,胡乐麻将辅助(大家学习交流开挂辅助神器)>>您好:软件加136704302中...
教会开挂!来来拼十辅助,潮汕掌... 教会开挂!来来拼十辅助,潮汕掌上娱破解器(辅助透视开挂辅助脚本)潮汕掌上娱破解器ai黑科技系统规律教...