简体   繁体   English

如何在Angular 5中将一个组件的变量更改为另一个组件的变量

[英]How to change variable of one component from another component in Angular 5

I am new in Angular and trying to achieve simple logic. 我是Angular的新手,正在尝试实现简单的逻辑。

enter image description here 在此处输入图片说明

sidebar.component.ts sidebar.component.ts

import { Component, OnInit,Input } from '@angular/core';
import { FormComponent } from 'app/form/form.component';

@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit {
  public serverStatus1 :boolean=false;
  public serverStatus2 :boolean=false;
  s1:number;
  s2:number=0;
  ngOnInit() {
        setInterval(() => {
        this.turnOnServers();
    }, 1000);
  }

  constructor() {

  }

  turnOnServers()
  {
    this.s2=this.s2+1;
    console.log(this.serverStatus1,"I am from turnOnServers",this.s2);
  }

  turnOnServer(id:string)
  {

    if(id==="1")
    {
      this.serverStatus1=!this.serverStatus1;
      console.log(this.serverStatus1,"I am from turnOnServer");
    }

    else if(id==="2")
    {
      this.serverStatus2=!this.serverStatus1;
      console.log(this.serverStatus2, "I am from turnOnServer");
    }
  }

form.component.ts form.component.ts

import { Component, OnInit,EventEmitter,Output,Input } from '@angular/core';
    import { NgForm } from '@angular/forms';
    import { SidebarComponent } from 'app/sidebar/sidebar.component';

    @Component({
      providers:[SidebarComponent],
      selector: 'app-form',
      templateUrl: './form.component.html',
      styleUrls: ['./form.component.css']
    })
    export class FormComponent implements OnInit {


      formstatus=false;
      user={
        username:"",
        password:""
      }
      constructor(private comp: SidebarComponent) { }

      ngOnInit() {
      }


      onSubmit(form:NgForm)
      {
        console.log(form);
        if(form.valid)
        {
          this.formstatus=true;
          this.user.username=form.value.username;
          this.user.password=form.value.password;

          if(form.value.username==="server1")
          {
            if(form.value.password==="s1")
            {
              this.comp.serverStatus1=!this.comp.serverStatus1;
              console.log(this.comp.serverStatus1,"I am from form" );

            }
          }

          if(form.value.username==="server2")
          {
            if(form.value.password==="s2")
            {
              this.comp.serverStatus2=!this.comp.serverStatus2;
              console.log(this.comp.serverStatus2,"I am from form" );

            }
          }
        }
      }

    }

These are above code for both the components. 以上是这两个组件的代码。 i am changing serverStatus2 from this -> this.comp.serverStatus2 of sidebar component the value changes for a sec but when setInterval() fetch the value from same variable it changes back to false. 我正在从侧边栏组件的this-> this.comp.serverStatus2中更改serverStatus2,该值会更改一秒,但是当setInterval()从同一变量中获取值时,它会变回false。 I don't know what is happening. 我不知道发生了什么

Components should be used for presentation, not storing data or app state. 组件应用于演示,而不是存储数据或应用程序状态。 This job is better suited for a service. 这项工作更适合提供服务。 Guide: https://angular.io/tutorial/toh-pt4 指南: https//angular.io/tutorial/toh-pt4

The best way would be to pass an observable of the data you want to pass to your other component. 最好的方法是将可观察的数据传递给其他组件。

const yourData$ = Observable.of(yourData);

And then pass this observable as an input of your other component. 然后将此可观察值作为您其他组件的输入。 In your component you subscribe to this observable so that you can update your component when the observable returns a new value 在您的组件中,您预订了该可观察对象,以便在该可观察对象返回新值时可以更新您的组件

yourData$.subscribe((newValue) =>   { 
valueUsedInComponent = newValue; 
});

Otherwise you could create a service that will make the two components communicate, using the same concept of subscribing to the observable of created In your parent component. 否则,您可以使用订阅可观察到的父组件中的可观察对象的相同概念,创建使两个组件进行通信的服务。

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

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