Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何将动态值附加到URL?_Angular - Fatal编程技术网

Angular 如何将动态值附加到URL?

Angular 如何将动态值附加到URL?,angular,Angular,我有一个Util.ts,它将包含所有API端点 在我的组件中,将导入Api类并使用Api 现在,其中一个API端点具有动态值,我需要将其附加到URL Utils.ts export class Api { public static PROGRESS_ISSUE = 'https://xxx/rest/api/2/search?jql=project%20%3D%2025764%20AND%20status%20%3D%20%22In%20Progress%22&fields=i

我有一个
Util.ts
,它将包含所有API端点

在我的组件中,将导入
Api
类并使用Api

现在,其中一个API端点具有动态值,我需要将其附加到URL

Utils.ts

export class Api {
    public static PROGRESS_ISSUE = 'https://xxx/rest/api/2/search?jql=project%20%3D%2025764%20AND%20status%20%3D%20%22In%20Progress%22&fields=id,key,status,project&maxResults=100'
}
import { Api } from './api/utils'; 

  getJiraOpenIssues(id: number, status:string) {
    return this.http.get<any>( Api.PROGRESS_ISSUE )
    .subscribe(count => console.log(count.total));
  }
在上面,我想将
项目
状态
作为动态值,我将在服务中传递这些值

服务.ts

export class Api {
    public static PROGRESS_ISSUE = 'https://xxx/rest/api/2/search?jql=project%20%3D%2025764%20AND%20status%20%3D%20%22In%20Progress%22&fields=id,key,status,project&maxResults=100'
}
import { Api } from './api/utils'; 

  getJiraOpenIssues(id: number, status:string) {
    return this.http.get<any>( Api.PROGRESS_ISSUE )
    .subscribe(count => console.log(count.total));
  }
从'/Api/utils'导入{Api};
getJiraOpenIssues(id:编号,状态:字符串){
返回此.http.get(Api.PROGRESS\u问题)
.subscribe(count=>console.log(count.total));
}

您可以简单地附加值,就像我们用+运算符连接字符串一样

   getJiraOpenIssues(id: number, status:string) {
        return this.http.get(Api.PROGRESS_ISSUE + '' + id + '/'+status)
            .map((response) => response.json());
    }

您可以在进度问题上放置一些占位符字符串,然后使用
replace
将其替换为实际值:

export class Api {
  public static PROGRESS_ISSUE = "https://xxx/rest/api/2/search?jql=project=PROJECTPLACEHOLDER&status=STATUSPLACEHOLDER&fields=id,key,status,project&maxResults=100"
}
然后,在
getJiraOpenIssues
方法中,使用replace:

import { Api } from './api/utils';

getJiraOpenIssues(id: number, status: string) {
  const apiUrl = Api.PROGRESS_ISSUE
    .replace('PROJECTPLACEHOLDER', 'YOUR PROJECT ID HERE')
    .replace('STATUSPLACEHOLDER', 'YOUR PROJECT STATUS HERE');
  return this.http.get<any>(apiUrl)
    .subscribe(count => console.log(count.total));
}
从'/Api/utils'导入{Api};
getJiraOpenIssues(id:编号,状态:字符串){
const apiUrl=Api.PROGRESS\u问题
.replace('PROJECTPLACEHOLDER','此处为您的项目ID')
.replace('STATUSPLACEHOLDER','YOUR PROJECT STATUS HERE');
返回此.http.get(APIRL)
.subscribe(count=>console.log(count.total));
}