电子上的Angular-使用可观察到UI的可观察
我正在尝试使用带有Angular的电子前端开发一个桌面应用程序。它利用后端的节点MSSQL软件包来查询数据库,并应以某种桌面的UI显示检索到的数据 - 为了该主题,可以说将在表中可视化。
目前,直到我要弄清楚它,应用程序会呈现出来:
具有文本输入以提供直接的SQL查询,将其发送的按钮以及通过ResultSet进行迭代。
组件的模板:
<input type="text" id="queryInput" margin="5,5,5,5" [placeholder]="placeholder" [(ngModel)]="queryTerm" />
<button class="button btn-info" (click)="onQuery()" margin="5,5,5,5">
Execute
</button>
<table id="dataTable" margin="5,5,5,5" class="table table-bordered" *ngFor="let item of shipments | async">
<tr>
<td>{{item.WMSObjectId}}</td>
<td>{{item.ShipNr}}</td>
<td>{{item.SupplyLetterNr}}</td>
<td>{{item.ArrivPeriodStartDT}}</td>
</tr>
</table>
这是组件类:
export class AppComponent implements OnInit {
title = 'Curiosity';
placeholder: string = 'Type query in here';
queryTerm: string = "";
queryResult: IShipment[] = [];
shipments: Observable<IShipment[]> | undefined;
constructor(private _sqlClientService: SQLClientService) {}
ngOnInit(): void {
this.shipments = new Observable<IShipment[]>((observer: Observer<IShipment[]>) => {
observer.next(this.getQueryResults());
});
}
onQuery(): void {
this._sqlClientService.sqlQuery(this.queryTerm, (response: IResponseObject) => this.updateResultSet(response));
}
updateResultSet(response : IResponseObject) : void{
this.queryResult = response.recordset;
console.log(this.queryResult);
}
getQueryResults() : IShipment[]{
return this.queryResult;
}
}
我努力克服的问题是让UI知道,有数据要呈现,因此 *ngfor可以填充桌子。
我可以看到数据检索已经完成,因为UpdaterEsultSet正在在控制台中显示它,但是我不太了解如何将其推向UI。
我该如何触发下一步()?
一旦我使用setInterval(()=&gt; observer.next(this.getQueryResults()),1000);
仅进行对其进行测试,然后它有效,但这不是正确的过程。
I am trying to develop a desktop app using Electron with Angular in front-end. It utilizes Node mssql package in back-end to query a database, and should present retrieved data in somewhat desktop-looking UI - for the sake of this topic, let's say it is going to be visualised in table.
For now, until i will figure it out, the app presents like this:
Has text input to provide direct sql query, button to send it , and table to iterate through resultset.
Component's template:
<input type="text" id="queryInput" margin="5,5,5,5" [placeholder]="placeholder" [(ngModel)]="queryTerm" />
<button class="button btn-info" (click)="onQuery()" margin="5,5,5,5">
Execute
</button>
<table id="dataTable" margin="5,5,5,5" class="table table-bordered" *ngFor="let item of shipments | async">
<tr>
<td>{{item.WMSObjectId}}</td>
<td>{{item.ShipNr}}</td>
<td>{{item.SupplyLetterNr}}</td>
<td>{{item.ArrivPeriodStartDT}}</td>
</tr>
</table>
Here is the component class:
export class AppComponent implements OnInit {
title = 'Curiosity';
placeholder: string = 'Type query in here';
queryTerm: string = "";
queryResult: IShipment[] = [];
shipments: Observable<IShipment[]> | undefined;
constructor(private _sqlClientService: SQLClientService) {}
ngOnInit(): void {
this.shipments = new Observable<IShipment[]>((observer: Observer<IShipment[]>) => {
observer.next(this.getQueryResults());
});
}
onQuery(): void {
this._sqlClientService.sqlQuery(this.queryTerm, (response: IResponseObject) => this.updateResultSet(response));
}
updateResultSet(response : IResponseObject) : void{
this.queryResult = response.recordset;
console.log(this.queryResult);
}
getQueryResults() : IShipment[]{
return this.queryResult;
}
}
The problem that im struggling to overcome is to let the UI know, that there is data to present, so *ngFor could populate the table.
I can see that data retrieval is completed, because updateResultSet is showing it in the console, but i don't quite understand how to push it to UI.
How exactly do i trigger next()?
Once i used setInterval(() => observer.next(this.getQueryResults()),1000);
just to test it, and THEN it works, but that's not a correct way ofcourse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在构造函数中导入更换eTectorRef并使用它,这会有所帮助吗?
https://angular.io/api/core/core/changedetectorref
If you import ChangeDetectorRef in the constructor and use it, would this help?
https://angular.io/api/core/ChangeDetectorRef
我将使用
evaryubject
来完成所需的行为。最好将其放入单独的服务中,并将其暴露在中,而不是在组件中管理它:以及模板的相关部分:
I would use a
BehaviorSubject
to accomplish the desired behavior. It would be best to put it in a separate service and expose itasObservable
instead of managing it in the component:And the relevant part of the template: