如何在angular2中订阅组件变量

时间:2016-08-30 07:43:20

标签: angular typescript

我使用我的服务器URL的标题,我在其中一个组件中声明它,我想在所有组件中使用该标头,我曾经有过服务器调用,但我没有订阅该标题,任何人都可以请帮助我

我的界面组件,

 import { Injectable } from '@angular/core';
 @Injectable()
export class Config {

 header = 'http://localhost/a2server/index.php/';

}

我的main.ts(这里我想使用标题)

 var data :any;
  constructor(public location: Location,public config:Config,public router: Router, public http: Http, fbld: FormBuilder,public toastr: ToastsManager) {

    this.config
        .subscribe(header => this.data = data); 
      console.log(this.data)

我知道我错了,有人可以帮助我吗

注射的,

  import { Injectable } from '@angular/core';
  import { Http, Response } from '@angular/http';
  import { Observable } from 'rxjs/Observable';
  import {Component} from '@angular/core';
  import { IDetails } from './details';
 import { Config } from '../headers';
 @Component({
   providers: [Config]
})
@Injectable()
 export class GetCustInfo {
   header:any;
   str = localStorage.getItem('social');
   loc = JSON.parse(this.str);
   id = this.loc.profile_id;
    constructor(private _http: Http,public config:Config) {  this.header = this.config.header;}
 private _productUrl = this.header+'profile/editcustominfo/'+this.id;
getCustList(): Observable<IDetails[]> {
    return this._http.get(this._productUrl)

.map((response: Response) => { 

    return <IDetails[]> response.json().data[0]; 

    });

}

}

1 个答案:

答案 0 :(得分:0)

您订阅了observables。对于一个简单的字符串,您只需访问该属性:

...
var key_value = mapStudent.Where(st => st.Key == classID).First();
var listStudent_EditFlag = key_value.Value;
foreach (KeyValuePair<List<Student>, Boolean> pair in listStudent_EditFlag)
{
    listStudent_EditFlag[pair.Key] = true;
    break;
}
...