简体   繁体   English

Angular Service将变量值传递给app.component

[英]Angular Service passing variable value to app.component

I have a service which contains a variable called url. 我有一个包含名为url的变量的服务。

Here is the code: 这是代码:

//service.ts

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import  'rxjs/add/operator/map';
import  'rxjs/add/operator/do';
import  'rxjs/add/operator/catch';

@Injectable()
export class DataService {

  private url = 'http://someurl.com/';

  constructor (private _http:Http){}

  getDataHttp():Observable<any> {
    return this._http.get(this.url)
      .map((response: Response) => <any> response.json())
      .do(data => console.log('All: ' +  JSON.stringify(data)))
      .catch(this.handleError);
  }

  private handleError(error: Response) {
    console.error(error);
    return Observable.throw(error.json().error || 'Server error');
  }

}

Here is the file where I want to call the url variable from the service: 这是我要从服务中调用url变量的文件:

// app.component.ts

import { Component, OnInit } from '@angular/core';
import { DataService } from './data.service';

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

  errorMessage;
  data;

  constructor(private service:DataService)  {}

  ngOnInit() {
    this.service.getDataHttp()
      .subscribe(data => this.data = data,
        error => this.errorMessage = <any>error);

  }


}

My question is ... How can I pass the url value from the service.ts to app.component.ts ? 我的问题是...如何将URL值从service.ts传递给app.component.ts?

For instance add getter/setter methods to DataService : 例如,将getter / setter方法添加到DataService

public getUrl(){
    return this.url;
}

public setUrl(url:string){
    this.url = url;
    return this.url;
}

and then call them like this from AppComponent : 然后从AppComponent这样调用它们:

let url = this.service.getUrl();

to retrieve the value, and: 检索值,并:

this.service.setUrl(another_url);

to set it. 设置它。

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

相关问题 Angular不呈现HTML中来自app.component的值 - Angular not rendering value from app.component in HTML 如何将属性从一个组件绑定到角度为4的App.Component? - How to Bind Property from one component to App.Component in angular 4? Angular Material sidenav指令在app.component中不起作用 - Angular Material sidenav directive does not work in app.component 如何将 app.component 的数据存储在 angular 中? - How to store data from app.component in angular? 如何在 app.component 的 Angular 中检测当前活动的父布局组件? - How to detect which parent layout component currently active in Angular in app.component? 在网页ionic 2中使用app.component函数 - Use app.component function in a page ionic 2 当我点击 angular 中 app.component 中的导航栏时,到达我的路由器插座的 div - Reach the div of my router-outlet when I click on the navigation bar in the app.component in angular 如何在app.component外使用带有router-outlet的Angular 9导航栏 - How to use Angular 9 navigation bar with router-outlet outside the app.component 为什么不在app.component中工作而在auth.guard中工作呢? - Why not working in app.component but in auth.guard? Angular 将参数传递给子组件会将该参数的值更改为父组件中的变量 - Angular Passing a parameter to a child component changes the value of that parameter as a variable in the parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM