【问题标题】:Angular2 HTTP GET parameter inside URLURL内的Angular2 HTTP GET参数
【发布时间】:2016-09-23 09:07:05
【问题描述】:

我想使用 Angular2 HTTP 和 URL 中的参数“searchId”调用 REST 服务 GET 方法,如下所示:

http://localhost:8080/services/name/:searchId/documents/

在 AngularJS 中这很容易:

       getDataResource(): ng.resource.IResourceClass<IEntityResource> {
        console.log("REST CALL");
        return this.$resource("http://localhost:8080/services/name/:searchId/documents/", {searchId: "12345678"}, {
            'query': {
                method: 'GET',
                isArray: false
            }
        });
    }

您能提出一个解决方案吗?

【问题讨论】:

    标签: angular


    【解决方案1】:

    不幸的是,唯一的方法是简单的连接:

    getData(searchId:number):Observable<any>{
        return this.http.get(http://localhost:8080/services/name/"+searchId.toString()+"/documents/";
    }
    

    关于Http Client documentation的更多信息。

    【讨论】:

      【解决方案2】:

      您可以使用模板字符串插值来实现此目的: 使用反勾号来实现它。所以不要使用"" 使用`` 而不是:searchId 使用$searchId

      return this.$resource(
      
      `http://localhost:8080/services/name/${searchId} /documents/`
      
      )
      .someMethod();
      

      更多信息请看here

      【讨论】:

      • 这不是有效的语法。它应该是字符串中的${ searchId }
      猜你喜欢
      • 2016-09-25
      • 2016-08-26
      • 2016-05-13
      • 2012-12-26
      • 2012-08-18
      • 2016-01-22
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      相关资源
      最近更新 更多