我正在尝试在angular2中构建chrome扩展,其中一部分将显示用户的chrome历史记录。组件:
/// <reference types="chrome/chrome-app"/>
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Component, OnInit } from '@angular/core';
import { mockHistory } from './chrome-history-mock';
@Component({
selector: 'app-chrome-history',
templateUrl: './chrome-history.component.html',
styleUrls: ['./chrome-history.component.css']
})
export class ChromeHistoryComponent implements OnInit {
historyData: any = [];
constructor(private http: Http) {}
ngOnInit() {
var microsecondsPerWeek = 1000 * 60 * 60 * 24 * 7;
var oneWeekAgo = (new Date).getTime() - microsecondsPerWeek;
chrome.history.search({
'text': '',
'startTime': oneWeekAgo,
// 'maxResults': 10
}, this.constructHistory);
}
constructHistory(historyObj): any {
console.log(historyObj[0]); //prints first obj
this.historyData = historyObj;
console.log(this.historyData.length); //prints 100
console.log(historyObj); //prints data
console.log(JSON.stringify(historyObj)); //prints data
console.log(JSON.stringify(this.historyData)); //prints data
}
}
模板:
<ul *ngIf="historyData?.length > 0">
<li *ngFor="let item of historyData | async">
<div class="history-item"><input type="checkbox" />{{item.title}}</div>
<span class="history-url">{{item.url}}</span>
</li>
我在回调函数constructHistory中获得响应,并且能够将其设置为类属性historyData,因为我可以在控制台日志中看到,但是这不会显示在模板中。我试图找到答案超过3天。搜索了很多博客,但无法找到任何解决方案。大多数是关于http.get案例中的数据绑定。也试过异步管但无法解决。抱歉,代码格式化。任何帮助表示赞赏。