回答編集履歴

5

追加コメントに対する回答

2018/11/22 09:19

投稿

BlueMoon
BlueMoon

スコア1339

test CHANGED
@@ -1,3 +1,27 @@
1
+ コメントに関してご回答します。
2
+
3
+
4
+
5
+ >DIするサービスの変数はそれぞれのコンポーネントで、共有できるという解釈でいいものでしょうか?
6
+
7
+
8
+
9
+ 基本的にその解釈で正しいです。ドキュメントを見ると、「CLIで生成されたアプリケーションでは、モジュールは事前ロードされプロバイダー(service)は全モジュールで利用可能となる」と有ります。そして「Angularルーターがモジュールを遅延ロードすると、新しいインジェクターが作成されます」とあり、遅延ロードしたComponentでは別インスタンスとなり共有できないものと解釈できます。
10
+
11
+
12
+
13
+ [https://angular.jp/guide/providers](https://angular.jp/guide/providers)
14
+
15
+
16
+
17
+ angularではページ部品をcomponentとすることでcomponentを組み合わせてページを作成したり、他ページで再利用することが可能です。そしてcomponent間のデータ受け渡しやイベントの通知にserviceをDIしてsubscribeを使用する方法が有ります。また、DB連動のためにrestサービスを使用する場合なども、serviceとして実装していれば、メソッドを各componentで共通利用できます。単にデータ共有する場合ではsubscribeを利用する必要は有りませんが、自componentで発生したイベントを他componentに通知したい時にはsubscribeを利用することになります。
18
+
19
+
20
+
21
+ ---
22
+
23
+
24
+
1
25
  実際に画面遷移するコードを書いて試してみました。結果、Routerを使用した画面遷移を行えばserviceは共有でき、serviceに保持用のメンバを配置してget/setterを使えばページ間のデータ受け渡しが出来ました。また、subscribeではデータ送信時(next)に受信側のComponentのインスタンスが不在なのでデータは受け取れないようです。ご参考までに確認したコードを記載します。
2
26
 
3
27
 

4

service記載漏れの補完

2018/11/22 09:19

投稿

BlueMoon
BlueMoon

スコア1339

test CHANGED
@@ -256,6 +256,54 @@
256
256
 
257
257
  ```
258
258
 
259
+ test.service.ts
260
+
261
+ ```javascript
262
+
263
+ import { Injectable } from '@angular/core';
264
+
265
+
266
+
267
+ @Injectable({
268
+
269
+ providedIn: 'root'
270
+
271
+ })
272
+
273
+
274
+
275
+ export class TestService {
276
+
277
+
278
+
279
+ private message:string;
280
+
281
+
282
+
283
+ getMessage():string{
284
+
285
+ return this.message;
286
+
287
+ }
288
+
289
+
290
+
291
+ setMessage(msg:string){
292
+
293
+ this.message=msg;
294
+
295
+ }
296
+
297
+
298
+
299
+ }
300
+
301
+
302
+
303
+ ```
304
+
305
+
306
+
259
307
  ---
260
308
 
261
309
  console.logの表示が正しいのであれば、subscribeのcallbackは出来ていると思えます。そうなるとComponent(class)とtemplate(html)のマッピングがどこかおかしいのではないでしょうか。

3

検証結果の報告

2018/11/21 09:04

投稿

BlueMoon
BlueMoon

スコア1339

test CHANGED
@@ -1,6 +1,262 @@
1
- こんは。
2
-
3
-
1
+ 実際画面遷移するコードを書いて試してみました。結果、Routerを使用した画面遷移を行えばservice共有でき、serviceに保持用のメンバを配置してget/setterを使えばページ間のデータ受け渡しが出来ましたまた、subscribeではデータ送信時(next)に受信側のComponentのインスタンスが不在なのでデータは受け取れないようです。ご参考までに確認したコードを記載します。
2
+
3
+
4
+
5
+ app.module.ts
6
+
7
+ ```javascript
8
+
9
+ import { BrowserModule } from '@angular/platform-browser';
10
+
11
+ import { NgModule } from '@angular/core';
12
+
13
+
14
+
15
+ import { AppRoutingModule } from './app-routing.module';
16
+
17
+ import { AppComponent } from './app.component';
18
+
19
+
20
+
21
+ import { SendComponent } from './send/send.component';
22
+
23
+ import { ReceiveComponent } from './receive/receive.component';
24
+
25
+ import {TestService} from './test.service'
26
+
27
+
28
+
29
+ @NgModule({
30
+
31
+ declarations: [
32
+
33
+ AppComponent,
34
+
35
+ SendComponent,
36
+
37
+ ReceiveComponent
38
+
39
+ ],
40
+
41
+ imports: [
42
+
43
+ BrowserModule,
44
+
45
+ AppRoutingModule
46
+
47
+ ],
48
+
49
+ providers: [TestService],
50
+
51
+ bootstrap: [AppComponent]
52
+
53
+ })
54
+
55
+ export class AppModule { }
56
+
57
+
58
+
59
+ ```
60
+
61
+ app-routing.module.ts
62
+
63
+ ```javascript
64
+
65
+ import { NgModule } from '@angular/core';
66
+
67
+ import { Routes, RouterModule } from '@angular/router';
68
+
69
+
70
+
71
+ import { SendComponent } from './send/send.component';
72
+
73
+ import { ReceiveComponent } from './receive/receive.component';
74
+
75
+
76
+
77
+ const routes: Routes = [
78
+
79
+ {path:'send',component:SendComponent},
80
+
81
+ {path:'receive',component:ReceiveComponent}
82
+
83
+ ];
84
+
85
+
86
+
87
+ @NgModule({
88
+
89
+ imports: [RouterModule.forRoot(routes)],
90
+
91
+ exports: [RouterModule]
92
+
93
+ })
94
+
95
+ export class AppRoutingModule { }
96
+
97
+ ```
98
+
99
+ app.component.ts
100
+
101
+ ```javascript
102
+
103
+ import { Component,OnInit } from '@angular/core';
104
+
105
+ import { Router } from '@angular/router';
106
+
107
+
108
+
109
+ @Component({
110
+
111
+ selector: 'app-root',
112
+
113
+ templateUrl: './app.component.html',
114
+
115
+ styleUrls: ['./app.component.css']
116
+
117
+ })
118
+
119
+ export class AppComponent implements OnInit{
120
+
121
+ title = 'tera01';
122
+
123
+
124
+
125
+ constructor(private router:Router) { }
126
+
127
+
128
+
129
+ ngOnInit(){
130
+
131
+ this.router.navigate(['send']);
132
+
133
+ }
134
+
135
+
136
+
137
+ }
138
+
139
+
140
+
141
+ ```
142
+
143
+
144
+
145
+ app.component.html
146
+
147
+ ```html
148
+
149
+ <router-outlet></router-outlet>
150
+
151
+ ```
152
+
153
+ send.component.ts
154
+
155
+ ```javascript
156
+
157
+ import { Component, OnInit } from '@angular/core';
158
+
159
+ import { Router } from '@angular/router';
160
+
161
+
162
+
163
+ import {TestService} from '../test.service'
164
+
165
+
166
+
167
+ @Component({
168
+
169
+ selector: 'app-send',
170
+
171
+ templateUrl: './send.component.html',
172
+
173
+ styleUrls: ['./send.component.css']
174
+
175
+ })
176
+
177
+ export class SendComponent implements OnInit {
178
+
179
+
180
+
181
+ constructor(private service:TestService,private router:Router) { }
182
+
183
+
184
+
185
+ ngOnInit() {
186
+
187
+ }
188
+
189
+
190
+
191
+ onClick(){
192
+
193
+ this.service.setMessage("from SendComponent");
194
+
195
+ this.router.navigate(['receive']);
196
+
197
+ }
198
+
199
+
200
+
201
+
202
+
203
+ }
204
+
205
+
206
+
207
+ ```
208
+
209
+ receive.component.ts
210
+
211
+ ```javascript
212
+
213
+ import { Component, OnInit } from '@angular/core';
214
+
215
+
216
+
217
+ import {TestService} from '../test.service'
218
+
219
+
220
+
221
+ @Component({
222
+
223
+ selector: 'app-receive',
224
+
225
+ templateUrl: './receive.component.html',
226
+
227
+ styleUrls: ['./receive.component.css']
228
+
229
+ })
230
+
231
+ export class ReceiveComponent implements OnInit {
232
+
233
+
234
+
235
+ msg:any;
236
+
237
+
238
+
239
+ constructor(private service:TestService) {
240
+
241
+ }
242
+
243
+
244
+
245
+ ngOnInit() {
246
+
247
+ this.msg = this.service.getMessage();
248
+
249
+ }
250
+
251
+
252
+
253
+ }
254
+
255
+
256
+
257
+ ```
258
+
259
+ ---
4
260
 
5
261
  console.logの表示が正しいのであれば、subscribeのcallbackは出来ていると思えます。そうなるとComponent(class)とtemplate(html)のマッピングがどこかおかしいのではないでしょうか。
6
262
 

2

思い違いについて記述

2018/11/21 08:58

投稿

BlueMoon
BlueMoon

スコア1339

test CHANGED
@@ -19,3 +19,11 @@
19
19
  this.aaaa = event; を this.aaaa = "test";と変えてみてブラウザ表示されるのであればtemplateとのマッピングは解決している(領域は共有している)と思います。bbbbでも成功しているようですし。そうなると引数で渡ってくるeventの内容(もしくは型)に表示できない要素があるのではと推測します。
20
20
 
21
21
  aaaa:anyと宣言してもNGだったでしょうか?
22
+
23
+
24
+
25
+ <追記>
26
+
27
+ すみません、初めから勘違いをしていたようです。画面遷移しているのですね(読み落としておりました)。SPAデザインと思いこんでいました。遷移先画面ではserviceをinjectしても別インスタンスになるものと思われます(試していないのですが恐らくそうでしょう)そうなると遷移前画面からのsubscribは読めないですね。前述の「領域は共有している」は撤回いたします。遷移先ページには他手段で値を渡す必要があると思います。
28
+
29
+ 「angular 画面遷移 データ受け渡し」で検索されてはどうでしょうか。

1

試行経過を見た思い付き

2018/11/21 04:40

投稿

BlueMoon
BlueMoon

スコア1339

test CHANGED
@@ -11,3 +11,11 @@
11
11
 
12
12
 
13
13
  aaaa以外に変数を定義し、templateと双方向バインドが出来ているか確認できますか?
14
+
15
+
16
+
17
+ <追記>
18
+
19
+ this.aaaa = event; を this.aaaa = "test";と変えてみてブラウザ表示されるのであればtemplateとのマッピングは解決している(領域は共有している)と思います。bbbbでも成功しているようですし。そうなると引数で渡ってくるeventの内容(もしくは型)に表示できない要素があるのではと推測します。
20
+
21
+ aaaa:anyと宣言してもNGだったでしょうか?