teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

タイトルを具体化

2021/01/09 05:14

投稿

rakiiiii
rakiiiii

スコア1

title CHANGED
@@ -1,1 +1,1 @@
1
- Angularでsubscribe内で取得した変数を外部から読み込みたい
1
+ AngularでHttpClientを利用して外部インターネットから取得した変数を別ファイルでも利用できるようにしたい
body CHANGED
File without changes

2

かなり抽象化していたコードを具体化しました。

2021/01/09 05:14

投稿

rakiiiii
rakiiiii

スコア1

title CHANGED
File without changes
body CHANGED
@@ -1,30 +1,54 @@
1
- # 前提/動作内容
1
+ # 前提
2
2
 
3
3
  AngularでWebアプリを開発している中での質問です。(超初心者です。)
4
4
 
5
- それぞれ、上段がservice.ts、下段がtest.component.tsというファイルです。
6
- test.component.tsではservice.tsで定義しているTestFuncという関数(Observable)参照して、this.responseとう値を取得ます。
5
+ Test.component.tsではAngularのHttpClientを利用して外部ネットワークに格納されているpageSize配列の0番目取得しています。(取得できています。
7
6
 
7
+
8
8
  #質問内容
9
- service.tsの「hoge = "10"」となっているところの"10"を、test.component.tsの「this.response」の値("10"が格納されているとします。)を代入したいのです。
9
+ TestService.tsの「postParam = postParam.set('pageSize','100')」となっているところの'100'を、test.component.tsの「this.response」の値('100'が格納されています。)を代入したいのです。
10
10
 
11
+ つまり、
12
+ 「postParam = postParam.set('pageSize',**'100'**)」を
11
- つまり、**hoge = "10"」を「hoge = this.response」のような形で表したい**のですが、方法があればお教えいただけないでしょうか。
13
+ postParam = postParam.set('pageSize',**this.response**)」のような形で表したいのですが、方法があればお教えいただけないでしょうか。
12
14
 
13
15
  何卒よろしくお願いいたします。
14
16
 
15
17
  ### 該当のソースコード
16
18
 
19
+
20
+ ### urlEncoder.ts
17
- ```Angular
21
+ ```TypeScript
22
+ import { HttpParameterCodec } from '@angular/common/http';
23
+
24
+ export class UrlEncoder implements HttpParameterCodec {
25
+ encodeKey(key: string): string {
26
+ return encodeURIComponent(key);
27
+ }
28
+ }
29
+ ```
18
- // Service.ts
30
+ ###TestService.ts
31
+ ```TypeScript
32
+ import { HttpClient, HttpParams } from '@angular/common/http';
33
+ import { UrlEncoder } from 'urlEncoder.tsのパス';
34
+
19
- export class TestService
35
+ export class TestService{
36
+ constructor(private http: HttpClient) { };
37
+
38
+ private setRequestParams(): HttpParams{
39
+ let httpParams: HttpParams = new HttpParams({ encoder: new CustomURLEncoder() });
40
+ httpParams = httpParams.set('hoge', '1'); //http://-----&hoge=1 になります。
41
+ }
42
+
20
- TestFunc(): Observable<any> {
43
+ paramsGet(path: string, postParams: any = {}): Observable<any> {
21
- let hoge: any
22
- hoge = "10";
44
+ let postParam: HttpParams = this.setRequestParams();
45
+ postParam = postParam.set('pageSize','100')//'100'の部分をthis.responseの値を参照したい。
46
+ return this.http.get(path, { params: postParam });
23
47
  }
24
48
  };
25
-
26
- ===============================================
49
+ ```
27
- //Test.component.ts
50
+ ###test.component.ts
51
+ ```TypeScript
28
52
  import TestService from {'Service.tsのパス'};
29
53
 
30
54
  @component{
@@ -35,23 +59,18 @@
35
59
  constructor(
36
60
  private test: TestService,
37
61
  )
38
-
39
- public response: any;
40
62
 
63
+ ngOnInit(){
41
- this.test.TestFunc().subscribe(res => {
64
+ this.test.paramsGet('http://------').subscribe(res => {
42
- this.response = res.array[0] //とき、this.responseには"10"が入っている
65
+ this.response = res.pageSize[0] //外部インターネット上pageSize配列の0番目を取得
66
+ }
43
- console.log(this.response) //"10"が表示される
67
+ )
44
68
  }
45
- console.log(this.response) // undefinedが表示される
46
69
 
47
-
48
- )
49
-
50
70
  }
51
-
52
71
  ```
53
72
 
54
73
  ### 試したこと
55
74
 
56
75
  初心者のため的外れかもしれませんが、this.response廻りをexportできないか試しましたがエラーでした。
57
- this.test.TestFunc().略( 略 )の外からはthis.test.TestFunc().略( 略 )内のthis.responseを参照できないことは理解しています。
76
+ this.test.paramsGet().略( 略 )の外からはthis.test.paramsGet().略( 略 )内のthis.responseを参照できないことは理解しています。

1

hoge = hoge.set("10")をhoge = "10"に変更しました。(記載ミスですが、質問内容は変わりません。)

2021/01/09 04:17

投稿

rakiiiii
rakiiiii

スコア1

title CHANGED
File without changes
body CHANGED
@@ -6,9 +6,9 @@
6
6
  test.component.tsではservice.tsで定義しているTestFuncという関数(Observable)を参照して、this.responseという値を取得します。
7
7
 
8
8
  #質問内容
9
- service.tsの「hoge = hoge.set("10")」となっているところの"10"を、test.component.tsの「this.response」の値("10"が格納されているとします。)を代入したいのです。
9
+ service.tsの「hoge = "10"」となっているところの"10"を、test.component.tsの「this.response」の値("10"が格納されているとします。)を代入したいのです。
10
10
 
11
- つまり、**「hoge = hoge.set("10")」を「hoge = hoge.set(this.response)」のような形で表したい**のですが、方法があればお教えいただけないでしょうか。
11
+ つまり、**「hoge = "10"」を「hoge = this.response」のような形で表したい**のですが、方法があればお教えいただけないでしょうか。
12
12
 
13
13
  何卒よろしくお願いいたします。
14
14
 
@@ -19,7 +19,7 @@
19
19
  export class TestService
20
20
  TestFunc(): Observable<any> {
21
21
  let hoge: any
22
- hoge = hoge.set("10");
22
+ hoge = "10";
23
23
  }
24
24
  };
25
25