简体   繁体   English

为什么NgFor在DOM中呈现“template_bindings = {}”而不是我的Component中的数据绑定元素?

[英]Why does NgFor render “template_bindings = {}” in the DOM instead of the data-bound elements in my Component?

When I inspect the data (cities and counties) in DevTools, it's being loaded properly. 当我检查DevTools中的数据(城市和县)时,它正在正确加载。 Everything on the template is loading properly too (no errors being thrown), except that the values of the option elements aren't rendering. 模板上的所有内容也正确加载(不会抛出任何错误),除了option元素的值不呈现。 Unfortunately, because Angular 2 just went beta, the syntax is different depending on when X blog post was written, or when Y SO question was asked, or even which page on angular.io you're looking at. 不幸的是,因为Angular 2刚刚进入测试版,所以语法是不同的,这取决于写博客文章的时间,或者当问到Y问题时,甚至是你正在看的angular.io上的哪个页面。 No idea what I'm missing here. 不知道我在这里缺少什么。

I realize assigning the value attr and adding text is probably overkill, but neither makes a difference. 我意识到分配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() {

        }

}

When I inspect the elements in DevTools, in the space where the option elements should be, I see this: 当我检查DevTools中的元素时,在option元素所在的空间中,我看到了:

<!--template bindings={}-->

A clue: I see that the comment is being rendered by setBindingDebugInfo in angular2/ts/src/platform/dom/dom_renderer.ts , which is "used only in debug mode to serialize property changes to comment nodes, such as <template> placeholders." 一条线索:我看到注释是由setBindingDebugInfo angular2/ts/src/platform/dom/dom_renderer.ts ,“仅在调试模式下用于序列化注释节点的属性更改,例如<template>占位符“。

The getRegions promise callback is not properly written getRegions promise回调未正确编写

getRegions(): void {
   this._api.getRegions().then((data: RegionalData) => { 
          this.cities = data.cities;
          this.counties = data.counties;
    });
}

use this notation, otherwise this refers to the callback function, instead of the GeoSelector class 使用这种表示法,否则this指的是回调函数,而不是GeoSelector类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM