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

时间:2018-11-11 12:16:25

标签: angular angular5 angular6 angular2-routing angular2-forms

我想将布尔值true发送给另一个模块组件,在该组件中,我将*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>

1 个答案:

答案 0 :(得分:1)

您应该使用Subject / BehaviorSubject。

服务:

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

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

组件1:

this.service.updateStatus(true);

第二部分:

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