简体   繁体   English

如何将一个组件中的变量用于 angular 中的另一个组件

[英]how to use the variable in one component to another component in angular

In angular application I have to use one variable which is defined in function of one component to another component在 angular 应用程序中,我必须使用一个在 function 中定义的变量,从一个组件到另一个组件

dashboard.component.ts仪表板.component.ts


export class DashboardComponent implements OnInit {
public nnum: string;
  public ncount: number = 0;

// some code

  addNotification(msg) {
    //const nl = document.querySelector("#notify");
    const nlnum = document.querySelector("#notificount");
    var el = document.createElement("li");
    if (msg.event == "New Signal") {
      if (wifiarray.indexOf(msg.mac) !== -1) {
        console.log("Already exists");
      } else {
        lcount = lcount + 1;
        this.ncount = lcount;

nav.component.ts导航组件.ts

import { Component, OnInit } from '@angular/core';
import { NotifyService } from '../../notify.service';


export class NavbarComponent implements OnInit {


}

nav.component.html nav.component.html



      <li class="nav-item dropdown" ngbDropdown>
        <a class="nav-link count-indicator dropdown-toggle" id="notificationDropdown" ngbDropdownToggle>
          
          <i class="fa fa-bell">
 
</i>

<span class="badge badge-pill badge-danger badge-up" >{{ncount}}</span>

        </a>
</li>

and I have the notify service notify.service我有通知服务notify.service

import { Injectable } from "@angular/core";
export interface Message {
 
}

Now I want to use the ncount value from dashboard component to navbar component to to view the value.现在我想使用从仪表板组件到导航栏组件的 ncount 值来查看该值。

I have tried but not working can anyone help me regarding this.我已经尝试但没有工作,任何人都可以帮助我解决这个问题。

Angular provides Input and Output decorators to pass data to and from parent and child components. Angular 提供InputOutput装饰器父组件和子组件传递数据。 Please find the reference link .请找到参考链接

If DashboardComponent is the child component of NavComponent , and you want to pass some variable data to its parent component, you'll have to use Output decorator in combination with EventEmitter , declared inside the class of DashboardComponent .如果DashboardComponentNavComponent的子组件,并且您想将一些可变数据传递给其父组件,则必须将Output装饰器与 EventEmitter 结合使用,在DashboardComponentclass中声明。 You'll declare something like this:您将声明如下内容:

@Output() myCount = new EventEmitter<number>();

Now wherever the value of your variable change, emit the same through this output decorator.现在,无论您的变量值发生变化,通过这个 output 装饰器发出相同的值。 Something like this:像这样的东西:

this.ncount = lcount; //from your sample code
this.myCount.emit(ncount);

Now in the parent component where DashboardComponent is declared, you'll have to declare this output emitter property, something like this:现在在声明DashboardComponent的父组件中,您必须声明此 output 发射器属性,如下所示:

<dashboard (myCount)="countChange($event)"></dashboard>

You will receive the updated ncount value in the countChange event like this.您将在这样的countChange事件中收到更新的ncount值。 (Note that you should have to declare countChange inside NavComponent.ts ): (请注意,您应该必须在NavComponent.ts 中声明 countChange

public countChange(newCount: number)
{
  console.log(newCount);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM