【发布时间】:2016-10-12 10:17:48
【问题描述】:
首先我是 Typescript 和 Angular 2 的新手,这似乎是一个显而易见的答案,但我似乎无法让它工作。我有以下型号
export interface IBrand {
brandID: number;
name: string;
image: string;
}
那么我就有了组件类
import { Component, OnInit } from '@angular/core';
import { Router, RouteParams } from '@angular/router-deprecated'
import { bootstrap } from '@angular/platform-browser-dynamic';
import { IBrand } from './brand';
import { BrandService } from './brand.service';
@Component({
selector: 'data-bind',
templateUrl: 'app/dashboardApp/brands/brand-list.component.html',
styleUrls: ['app/dashboardApp/brands/brand-list.component.css']
})
export class BrandListComponent implements OnInit {
brands: IBrand[];
errorMessage: string;
newBrand: IBrand;
pageTitle: string = 'Brands';
constructor(private _brandService: BrandService,
private _router: Router) {
}
ngOnInit(): void {
this._brandService.getBrands()
.subscribe(
brands => this.brands = brands,
error => this.errorMessage = <any>error);
}
}
然后我有以下html
<div class="form-group">
<label for="brandName">Brand:</label>
<input type="text" class="form-control" placeholder="Name" id="brandName" [(ngModel)]="newBrand.name" />
</div>
我无法让 IBrand 的属性成功运行并出现错误
platform-browser.umd.js:962 ORIGINAL EXCEPTION: TypeError: Cannot read property 'name' of undefined
但是我可以轻松地将它绑定到 pageTitle 之类的东西。有什么想法可以为我指明正确的方向吗?
【问题讨论】:
标签: angular