在另一个js文件中调用角度服务请求

时间:2018-12-19 18:04:52

标签: javascript angular canvas three.js

我的角度项目中有几个html和打字稿文件,并且有一个专用于three.js图形部分的软件包。我需要从我的Canvas.js文件中的Service调用一个函数,以向数据库发出GET请求,并根据该数据请求创建3d对象。

item.service.ts

import { Injectable } from '@angular/core';
import { Item } from './item';
import { Observable, of } from 'rxjs';
import { MessageService } from './message.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError, map, tap } from 'rxjs/operators';
const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable({
  providedIn: 'root'
})
export class ItemService {

  item: Item;
  private itemURL = '';

  constructor(private http: HttpClient, private messageService: MessageService) { }

  getItemById(_id: string): Observable<string> {
    const url = `${this.item}/${_id}`;
    return this.http.get<string>(url);
  }

Canvas.js

function onLoad() {
    // var injector = angular.element(document).injector()

    angular.element('*[ng-app]').injector().get("ItemService").getItemById(13);
    //possible alternatives
    //angular.element(document).find('body').injector().get("ItemService");  
    //getSrv("ItemService", document.body);


    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

我需要在getItemById()中调用Canvas.js方法,并将该数据保存到变量中并使用其属性。

也:my project structure

谢谢

0 个答案:

没有答案