Angular 2 - ng-select - 从 JSON 文件中获取数据
Angular 2 - ng-select - get datas from a JSON file
我安装了 ng-select 组件,它允许我在可编辑的 select.
中显示自动完成器
我试图通过 JSON 获取此 select 的选项,但这对我不起作用!
这项工作 - 组成部分:
prodFilterDescr: Array<IOption> = [
{label: 'Belgium', value: 'Belgium'},
{label: 'Luxembourg', value: 'Luxembourg'},
{label: 'Netherlands', value: 'Netherlands'}
];
Html:
...<ng-select
[options]="prodFilterDescr"
placeholder="Prodotto"
>
</ng-select>...
这不起作用 - 组件(在构造函数中):
this.http.get('app/json/filtriProdotti.json')
.subscribe(res => this.filters = res.json());
var filter = [];
this.filters.forEach(function(item){
filter.push(item.tipoProdottoId)
})
console.log(filter)
let typeProdFilter: Array<IOption> = filter;
console.log(typeProdFilter)
Html:
<ng-select
[options]="typeProdFilter"
placeholder="Tipo prodotto"
>
</ng-select>
似乎无法读取在构造函数或其他方法中写入的内容...我怎样才能让我的 "options" 到达我的 JSON 数据?
问题与 async
调用 http.get
方法有关
尝试运行这个代码:
// define this outside of constructor
typeProdFilter: Array<IOption>;
// keep this inside the constructor
this.http.get('app/json/filtriProdotti.json')
.subscribe(res =>
{
this.filters = res.json()
var filter = [];
this.filters.forEach(function(item){
filter.push(item.tipoProdottoId)
})
console.log(filter)
this.typeProdFilter = filter;
console.log(this.typeProdFilter)
});
要动态加载选项,请同时查看:
https://basvandenberg.github.io/ng-select/examples/load-options
我安装了 ng-select 组件,它允许我在可编辑的 select.
中显示自动完成器我试图通过 JSON 获取此 select 的选项,但这对我不起作用!
这项工作 - 组成部分:
prodFilterDescr: Array<IOption> = [
{label: 'Belgium', value: 'Belgium'},
{label: 'Luxembourg', value: 'Luxembourg'},
{label: 'Netherlands', value: 'Netherlands'}
];
Html:
...<ng-select
[options]="prodFilterDescr"
placeholder="Prodotto"
>
</ng-select>...
这不起作用 - 组件(在构造函数中):
this.http.get('app/json/filtriProdotti.json')
.subscribe(res => this.filters = res.json());
var filter = [];
this.filters.forEach(function(item){
filter.push(item.tipoProdottoId)
})
console.log(filter)
let typeProdFilter: Array<IOption> = filter;
console.log(typeProdFilter)
Html:
<ng-select
[options]="typeProdFilter"
placeholder="Tipo prodotto"
>
</ng-select>
似乎无法读取在构造函数或其他方法中写入的内容...我怎样才能让我的 "options" 到达我的 JSON 数据?
问题与 async
调用 http.get
方法有关
尝试运行这个代码:
// define this outside of constructor
typeProdFilter: Array<IOption>;
// keep this inside the constructor
this.http.get('app/json/filtriProdotti.json')
.subscribe(res =>
{
this.filters = res.json()
var filter = [];
this.filters.forEach(function(item){
filter.push(item.tipoProdottoId)
})
console.log(filter)
this.typeProdFilter = filter;
console.log(this.typeProdFilter)
});
要动态加载选项,请同时查看:
https://basvandenberg.github.io/ng-select/examples/load-options