我有一个要求,其中从外部输入领域之外角我得到的输入值,使用它我引发了我的角模块服务,从后端获取相关数据。 订阅功能工作正常进行内部的变化,但由于这里的输入值从全局函数来,该UI是没有得到呈现。
我曾尝试zone.run
, detectChange
和detectCheck
但似乎没有被更新UI。 该UI应该显示使用的项目列表suggestedItems
阵列。 请让我知道什么是错。
import { Component, OnInit, HostListener, NgZone, ChangeDetectorRef } from '@angular/core';
import { BackendApiService } from '../services/backend-api.service';
import { WindowAccessService } from '../services/window-access.service';
@Component({
selector: 'app-search-suggestion',
templateUrl: './search-suggestion.component.html',
styleUrls: ['./search-suggestion.component.scss']
})
export class SearchSuggestionComponent implements OnInit {
suggestionHeaders = ['Item', 'Item name', 'Item category', 'Item details'];
suggestedItems; dummyRequestObj; value;
calledFromOutside(newValue: String) {
this.zone.run(() => {
this.value = newValue;
this.dummyRequestObj.conditions = [];
this.dummyRequestObj.conditions.push({
'column': 'SEARCH_BY_NAME',
'value': this.value
});
// this.chngDt.detectChanges();
this.items.getData(this.dummyRequestObj, false, false);
console.log(this.value);
});
}
// tslint:disable-next-line:max-line-length
constructor( public items: BackendApiService, private zone: NgZone, public refWin: WindowAccessService, private chngDt: ChangeDetectorRef) {
this.suggestedItems = new Array<any>();
}
ngOnInit() {
this.items.updateItemList.subscribe(
(data) => {
console.log('was triggered');
this.zone.run(() => {
this.suggestedItems = data;
});
// this.chngDt.markForCheck();
console.log('data', this.suggestedItems);
}
);
this.refWin.nativeWindow.angularComponentRef = {
zone: this.zone,
componentFn: (value) => this.calledFromOutside(value),
component: this
};
this.dummyRequestObj = {
"preciseSearch": false,
"useFuzzy": false,
"mode": 0,
"startIndex": 0,
"noOfRecords": 12,
"ascending": false
};
}
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th *ngFor="let header of suggestionHeaders">{{header}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of suggestedItems">
<td>{{item.erpPartNumber}}</td>
<td>{{item.itemShortDesc}}</td>
<td>{{item.productCategoryName}}</td>
<td>{{item.itemDesc}}</td>
</tr>
</tbody>
</table>
</div>