我正在尝试如何传递消息app.component.ts来显示messageComponent.ts
在app.component.html我添加<app-messagecomponent></app-messagecomponent>
添加它什么都没有显示的那一刻.
我在服务中也有一个方法:
message.service.ts
message(message) {
return message;
}
Run Code Online (Sandbox Code Playgroud)
所以我想通过消息服务从其他组件传递消息,以便在app-messagecomponent.html中显示
例如,来自app.component.ts:
sendMessageToService() {
this.myservice.message('my Message to be displayed in the messageComponent');
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我有这个服务使用HttpClient来获取一些数据:
checkData() {
return this.http.get('my url');
}
Run Code Online (Sandbox Code Playgroud)
我在脚注组件上调用它并显示结果:
ngOnInit() {
this.myservice.checkdata().subscribe( result => { this.statustext = result } );
}
Run Code Online (Sandbox Code Playgroud)
这有效,但我需要每10秒运行一次这样的方法,因此它是最新的.
我怎样才能做到这一点?
我有一些看起来像这样的数据:
在我身上app.component.html我有这个:
<ul>
<li *ngFor="let data of myData">{{data.id}}</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
当我运行它时它显示列表但没有任何值,所以我只是从中得到了很多点 <li>
在我的app.component.ts身上:
myData;
Run Code Online (Sandbox Code Playgroud)
然后:
this.myData = obj; // Obj contains the data
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
我有一个服务,在构造函数中,我正在运行一种方法来获取一些数据。我可以看到数据,然后将其传递给预定义的变量。
像这样:
export class SentinelService {
configuration;
constructor() {
this.electronService.ipcRenderer.on('config' , function(event , data) {
this.configuration = data; // pass the data to configuration variable
console.log(this.configuration.name); // Check it ... I can see the value here
});
}
myMethod() {
// Try it here
console.log(this.configuration.name); // I get Undefined
};
...
Run Code Online (Sandbox Code Playgroud)
尽管我已经将值分配给“ configuration”变量,并且可以看到它是从构造函数内部的方法传递的,但是当我在另一个方法上尝试相同的操作时,却无法定义。
我怎样才能解决这个问题?