Angular 2 - 未捕获( promise ): TypeError: Cannot read property 'title' of undefined

标签 angular

当我尝试添加一些具有双重绑定(bind)的文本输入字段时,我在我的应用程序中看到了这个错误:

   ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'title' of undefined
TypeError: Cannot read property 'title' of undefined
    at Object.eval [as updateRenderer] (ng:///AppModule/TapesComponent.ngfactory.js:660:29)
    at Object.debugUpdateRenderer [as updateRenderer] (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12658:21)
    at checkAndUpdateView (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12037:14)
    at callViewAction (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12347:17)
    at execComponentViewsAction (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12293:13)
    at checkAndUpdateView (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12038:5)
    at callWithDebugContext (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:13020:42)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12560:12)
    at ViewRef_.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:10129:63)
    at RouterOutlet.activateWith (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:5378:42)
    at ActivateRoutes.placeComponentIntoOutlet (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4558:16)
    at ActivateRoutes.activateRoutes (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4539:26)
    at eval (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4475:58)
    at Array.forEach (native)
    at ActivateRoutes.activateChildRoutes (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4475:29)
    at Object.eval [as updateRenderer] (ng:///AppModule/TapesComponent.ngfactory.js:660:29)
    at Object.debugUpdateRenderer [as updateRenderer] (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12658:21)
    at checkAndUpdateView (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12037:14)
    at callViewAction (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12347:17)
    at execComponentViewsAction (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12293:13)
    at checkAndUpdateView (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12038:5)
    at callWithDebugContext (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:13020:42)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:12560:12)
    at ViewRef_.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:10129:63)
    at RouterOutlet.activateWith (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:5378:42)
    at ActivateRoutes.placeComponentIntoOutlet (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4558:16)
    at ActivateRoutes.activateRoutes (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4539:26)
    at eval (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4475:58)
    at Array.forEach (native)
    at ActivateRoutes.activateChildRoutes (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:4475:29)
    at resolvePromise (http://localhost:3000/node_modules/zone.js/dist/zone.js:712:31) [angular]
    at resolvePromise (http://localhost:3000/node_modules/zone.js/dist/zone.js:683:17) [angular]
    at http://localhost:3000/node_modules/zone.js/dist/zone.js:760:17 [angular]
    at Object.onInvokeTask (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:4123:37) [angular]
    at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:397:36) [angular]
    at Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:165:47) [<root> => angular]
    at drainMicroTaskQueue (http://localhost:3000/node_modules/zone.js/dist/zone.js:593:35) [<root>]
    at HTMLAnchorElement.ZoneTask.invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:464:25) [<root>]

我有这个 Tape 类:

    export class Tape {
  constructor (
  public id: number,
  public title: string,
  public date?: string,
  public rating?: number,
  public description?: string,
  public photo?: string){}
}

和组件

import { Component, OnInit } from '@angular/core';
import { Tape } from './tape'
import { TapeService } from './tape.service';
import { Router } from '@angular/router';
// import { TitlePipe } from './titleFilter';

@Component({
  selector: 'my-tapes',
  templateUrl: './tapes.component.html',
  styleUrls: [`./tapes.component.css`],
  providers: [TapeService],

})
export class TapesComponent implements OnInit {
  pageName = 'VHS Movies listing';
  tapes: Tape[];
  selectedTape: Tape;
  model = new Tape(0,'s','s',0,'s','sS'); 

组件 HTML 看起来像

<div class="form-group">
              <label for="title">Title{{model.title}}</label>
              <input type="text" class="form-control" id="title" required [(ngModel)]="model.title" name="title" #title="ngModel">              Written text: {{tape.title}}
              <div [hidden]="title.valid || title.pristine" class="alert alert-danger">
                Title is mandatory
              </div>
            </div>

我刚开始学习 Angular 2,但对我来说它应该从模块中获得标题。他他看不懂标题?

最佳答案

尝试使用 safe/elvis 运算符检查值是否存在然后显示,

 <input type="text" class="form-control" id="title" required [(ngModel)]="model?.title" name="title" #title="ngModel"> Written text: {{model?.title}}

关于Angular 2 - 未捕获( promise ): TypeError: Cannot read property 'title' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43292680/

相关文章:

data-binding - 绑定(bind)在 Angular2 模板中不起作用

angular - 如何在Angular中使用catchErroŕRxJS运算符进行错误处理?

Angular 4 上传到 Symfony REST Api

javascript - 如何在 ng-template 中使用响应式表单

javascript - 使用momentjs显示当前时间

css - Angular:表格标题上的粘性东西

html - 有没有办法突出显示点击的 mat-tab?当动态生成每个 mat-tab 时

java - Spring Boot 无法检测我输入的值

Angular Material 最大长度验证

javascript - angular4 viewchild 不在 dom 元素上工作