【发布时间】:2018-10-22 04:34:52
【问题描述】:
在我当前的 Angular 5 项目中,我一直在通过我的服务成功调用 API
(代码保持简洁)
myService.ts :
@Injectable()
export class my-service-service {
constructor(private _http: HttpClient) {
}
myData1() {
return this._http.get("API_URL")
.map(result => result);
}
并通过订阅上述服务访问有关我连接的组件的信息。
myComponent.ts:
import { myServiceService } from './my-service-service.service';
@Component({
...
})
constructor(private router: Router,
private route: ActivatedRoute,
private _myData: myServiceService) {
...
}
ngOnInit() {
this._myData.myData1()
.subscribe(res => {
//Able to access data successfully here
}
}
现在我希望从我的控制器向服务发送相反方向的数据。 例如假设我有
public myVar: any;
我希望从我的服务中访问这个变量(也许将它动态附加到 API 调用中) 我应该继续这样做怎么样。
【问题讨论】:
标签: angular service components angular5 web-development-server