为什么我的angular订阅方法不起作用?

问题描述 投票:0回答:1

我是Angular,Nodejs的新手,我在做txt文件的CRUD应用我必须在列表中列出所有文件,如果您单击一个文件,则需要在“ P”段上显示数据我的文档组件getDocuments()-返回路径中所有文件的数组getOneDOcument(name)->获取给定名称的文件的所有数据getData(name)->必须将数据发送到ID为_>“ paragraph_content”

的段落

  getDocuments(){
    this.DocumentService.getDocuments()
        .subscribe(res => {
          this.DocumentService.documents = res as Document[]
          console.log("dentro del susbcribe");
          console.log(res);
        });
    console.log("Fuera del subscribe");
  }

  getOneDocument(name : String){
    console.log("NOO");

    this.DocumentService.getOneDocument(name).subscribe(res => {
            this.DocumentService.documentSelected = res as Document;          
            console.log("."); 
        });

  }

  getData(name : String){
    console.log("hello name -> " , name)
    // document.getElementById("paragraph_content").innerHTML = this.DocumentService.getOneDocument(name)

    console.log("Before the subscrie");
    this.DocumentService.getOneDocument(name)
        .subscribe( (res ) =>{
          //I need to change the paragraph content like this
          //document.getElementById("paragraph_content").innerHTML = res.toString() Not working
          console.log("Within", res);
        } )
    console.log("After Subscribe ")
  }

文档服务我得到了给定的URL数组


  getDocuments(){
    console.log("Get Documents method");

   return this.http.get(this.URL_API );
  }
  getOneDocument(name: String){
    console.log("Get OneDocyment method name given: " , name);
    return this.http.get(this.URL_API + `/${name}`)
  }

  postDocument(){
   //left
  }

  deleteDocument(name:String){
   //left
  }

文档组件html

<nav>
        <ul class="ds-list-unstyled" *ngFor="let document of DocumentService.documents">
            <li><a href="#" (click)="getData(document)"> {{document}}  </a></li>
        </ul>
    </nav>

    <article>
        <h2>Texto en pantalla: </h2>
        <p id="paragraph_content">Needs to change with a click
        </p>

    </article>

单击文件时得到的响应是:Image1:

提前感谢

node.js angular observable subscribe
1个回答
0
投票

我认为您的问题是,您未指定数据为文本,因此正试图转换为JSON。

尝试将responseType添加到请求中,类似这样,

getOneDocument(name: String){
    return this.http.get(`${this.URL_API}/${name}`, {responseType: 'text'})
  }
© www.soinside.com 2019 - 2024. All rights reserved.