* ngIf的订阅值未更新

时间:2019-08-15 03:06:32

标签: angular angular-services angular-ng-if

这是我正在尝试做的基本想法。!

我有一个全局导航栏,用于我的应用程序中的所有模块。但是当我转到特定组件时,我想从导航栏中隐藏搜索栏。为此,我使用了创建全局变量并订阅的方法(从Navbar订阅)

location.service.ts是我的全局变量服务

import { Injectable } from '@angular/core';
import { Observable ,of as observableOf} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class LocationService {
  private createnewproject: boolean = false;
  constructor() { }

public getcurrentlocation(): Observable<boolean>
{
  return  observableOf( this.createnewproject);
}

public setcurrentlocation(status:boolean)
{
  this.createnewproject = status ;
}

public checkvalue():boolean
{
  return this.createnewproject;
}

}

// createnewproject最初为false,一旦转到特定模块,我将其设置为true

这是我的Navbar类,这是我订阅它的方式

import { Component, OnInit } from '@angular/core';
import { ConstantsService } from '../common/services/constants.service';
import { LocationService} from '../common/services/location.service';
import { observable } from 'rxjs';

@Component({
  selector: 'app-ibrainmart-header',
  templateUrl: './ibrainmart-header.component.html',
  styleUrls: ['./ibrainmart-header.component.css']
})
export class IbrainmartHeaderComponent implements OnInit {
  oncreateproject: boolean;
  constructor( private projectloaction: LocationService) { }

  ngOnInit()
  {


    const oncreate = this.projectloaction.getcurrentlocation().subscribe(observable => { this.oncreateproject = observable;});

  }

}

这是我计划使用* ngIf

从代码中隐藏搜索栏的方式
 <div class="row" *ngIf = "!oncreateproject">
      <div class="col-md-12">
          <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
                  <div class="input-group mb-3">
                  <input type="text" class="form-control"  placeholder="Search anything ..!" aria-label="Searchbar" aria-describedby="">
                      <div class="input-group-append">
                      <button class="btn btn-outline-light" type="button" id="btnsearch" ><i class="fa fa-search"></i>{{oncreateproject}}</button>
                      </div>


                  </div>
          </nav>
      </div>
  </div>

这是我一次到达特定页面后将全局变量设置为true的方式

import { Component, OnInit } from '@angular/core';
import { ConstantsService } from 'src/app/common/services/constants.service';
import { LocationService } from 'src/app/common/services/location.service';

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

  createnewproject: boolean;

  constructor( private oncreateproject: LocationService) {

   this.oncreateproject.setcurrentlocation(true);
   console.log('Globle Value:' + this.oncreateproject.checkvalue());
   }

  ngOnInit()
  {

  }


}

但是当我转到此页面时,全局值正在更新,但Serach栏没有隐藏,有人可以帮助我找到我在哪里做错了吗??

2 个答案:

答案 0 :(得分:2)

您需要使用rxjs主题的function只是创建一个可观察的值基,但不会将对属性的任何更改同步,这就是为什么我认为使用Subject是最好的情况

import { Injectable } from '@angular/core';
import { Observable ,Subject , from} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class LocationService {
  private createnewproject: Subject<boolean>= new Subject();
  constructor() { }


public getcurrentlocation(): Observable<boolean>
{
  return  from(this.createnewproject);
}

public setcurrentlocation(status:boolean)
{
  this.createnewproject.next(status);
}


}

IbrainmartHeaderComponent

export class IbrainmartHeaderComponent implements OnInit {

  // set initial value in case you want to see the search bar by default 
  oncreateproject: boolean = true;

  constructor( private projectloaction: LocationService) { }

  ngOnInit()
  {
    this.projectloaction.getcurrentlocation().subscribe(state => { 
      this.oncreateproject = state;
    });

  }

}

subjects

答案 1 :(得分:0)

我将使用BehaviorSubject,以便它拥有一个初始值,并且可以在视图中使用async,以便解析该值。

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { BehaviorSubject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class LocationService {
  private searchBarVisibility$ = new BehaviorSubject(true);
  constructor() { }

  public getcurrentlocation(): Observable<boolean> {
    return from(this.searchBarVisibility$);
  }

  public setcurrentlocation(status: boolean) {
    this.searchBarVisibility$.next(status);
  }

}

然后隐藏搜索栏

 <div class="row" *ngIf = "oncreateproject | async">
      <div class="col-md-12">
          <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
                  <div class="input-group mb-3">
                  <input type="text" class="form-control"  placeholder="Search anything ..!" aria-label="Searchbar" aria-describedby="">
                      <div class="input-group-append">
                      <button class="btn btn-outline-light" type="button" id="btnsearch" ><i class="fa fa-search"></i>{{oncreateproject | async}}</button>
                      </div>
                  </div>
          </nav>
      </div>
  </div>

IbrainmartheaderComponent

import { Component, OnInit } from '@angular/core';
// import { ConstantsService } from '../constants.service';
import { LocationService} from '../location.service';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-ibrainmart-header',
  templateUrl: './ibrainmart-header.component.html',
  styleUrls: ['./ibrainmart-header.component.css']
})
export class IbrainmartheaderComponent implements OnInit {
  oncreateproject: Observable<boolean>;
  constructor( private projectlocation: LocationService) { }

  ngOnInit()
  {
    this.oncreateproject = this.projectlocation.getcurrentlocation();
  }

}

CreatenewprojectComponent

import { Component, OnInit } from '@angular/core';
// import { ConstantsService } from 'src/app/common/services/constants.service';
import { LocationService } from '../location.service';

@Component({
  selector: 'app-createnewproject',
  templateUrl: './createnew-project.component.html',
  styleUrls: ['./createnew-project.component.css']
})
export class CreatenewprojectComponent {

  createnewproject: boolean;

  constructor(private projectlocation: LocationService) {
    this.projectlocation.getcurrentlocation().subscribe(value =>
          console.log('Globle Value:' + value)
    );
    this.projectlocation.setcurrentlocation(true);
  }

}