[英]Why does NgFor render “template_bindings = {}” in the DOM instead of the data-bound elements in my Component?
當我檢查DevTools中的數據(城市和縣)時,它正在正確加載。 模板上的所有內容也正確加載(不會拋出任何錯誤),除了option
元素的值不呈現。 不幸的是,因為Angular 2剛剛進入測試版,所以語法是不同的,這取決於寫博客文章的時間,或者當問到Y問題時,甚至是你正在看的angular.io上的哪個頁面。 不知道我在這里缺少什么。
我意識到分配value
ATTR和添加文字可能是矯枉過正,但也有差別。
import {Component} from 'angular2/core';
import {NgFor} from 'angular2/common';
import {City, County, RegionalData} from './interfaces';
import {api} from './api.service';
@Component({
selector: 'geo-selector',
template: `
<div>
<select name="region-selector">
<optgroup label="Cities">
<option *ngFor="#city of cities" selected="selected" [value]="city.name">{{city.name}}</option>
</optgroup>
<optgroup label="Counties">
<option *ngFor="#county of counties" [value]="county.name">{{county.name}}</option>
</optgroup>
</select>
<label for="">Zip Search</label>
<input type="text" name="zip-search"/>
</div>
`,
providers: [api],
directives: [NgFor]
})
export class GeoSelector {
public cities: City[];
public counties: County[];
constructor(private _api:api) {
this.getRegions();
}
getRegions(): void {
this._api.getRegions().then(function(data: RegionalData) {
this.cities = data.cities;
this.counties = data.counties;
});
}
onSelect() {
}
}
當我檢查DevTools中的元素時,在option
元素所在的空間中,我看到了:
<!--template bindings={}-->
一條線索:我看到注釋是由setBindingDebugInfo
angular2/ts/src/platform/dom/dom_renderer.ts
,“僅在調試模式下用於序列化注釋節點的屬性更改,例如<template>占位符“。
getRegions
promise回調未正確編寫
getRegions(): void {
this._api.getRegions().then((data: RegionalData) => {
this.cities = data.cities;
this.counties = data.counties;
});
}
使用這種表示法,否則this
指的是回調函數,而不是GeoSelector類
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.