繁体   English   中英

无法将数据从一个模块组件发送到另一模块组件

[英]not able to send the data from one module component to another module component

我想将boolean值发送给另一个模块组件,在该组件中,我应将*ngIf值设置为true以启用html内容。 它正在导航到另一个组件,但无法启用html内容。 我已经尝试过使用viewchild,发射极,输入,输出,但是无法达到预期的效果。

调用组件:

SuccessGet(res) {
    this.logs = res;

    for(let item of this.logs)
    {
        if(item._email == this.log._email && item._password == this.log._password)
        {
            alert("Login Successful");

            this.wt.LoginCall(true);     // here calling the other module component
        }
    }

    this.log = new LoginModel();
}

调用的组件:

export class WeatherComponent {
      title = 'TemperatureApp';

      //@Input() messagetochild_weather ;//to get the message from parent

      showweather : boolean = false;
      // showweather : boolean = false;

      bus : BusinessLogic = new BusinessLogic();
      WeatherModel : Weather = new Weather();
      WeatherModels : Array<Weather> = new Array<Weather>();
      num : number = 0;

      LoginCall(item : boolean) 
      {
          //this.ngAfterViewInit();
          this.showweather = item;
      }

      ngAfterViewInit(item : any): void
      {
          this.WeatherModels = this.bus.Load(); 
      }

HTML:

<div *ngIf=showweather class="card">//here i am using *ngIf
  <h3 class="card-header text-center font-weight-bold text-uppercase py-4">Editable table</h3>
  <div class="card-body">
    <div id="table" class="table-editable">
      <span class="table-add float-right mb-3 mr-2"><a href="#!" class="text-success"><i class="fa fa-plus fa-2x"
            aria-hidden="true"></i></a></span>
      <table class=" table table-bordered table-responsive-md table-striped text-center table-responsive-sm" style="border-block-end-color:dodgerblue">
        <tr style="background-color: rgb(0, 255, 255)">
          <th class="text-center">Area Code</th>
          <th class="text-center" >Date</th>
          <th class="text-center">Country</th>
          <th class="text-center">State</th>
          <th class="text-center">City</th>
          <th class="text-center">Temperature</th>
          <th class="text-center">Action</th>
          <th class="text-center">Action</th>
        </tr>

您应该使用Subject / BehaviorSubject。

服务:

private statusValue = new Subject<Boolean>();
statusValue$ = this.statusValue.asObservable();

updateStatus(status){
  this.statusValue.next(status);
}

组件1:

this.service.updateStatus(true);

COMPONENT2:

this.service.statusValue$.subscribe(value=>{console.log(value);}

暂无
暂无

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

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