简体   繁体   English

Angular 2中的类模型不起作用

[英]Class Model in Angular 2 not working

I have a small problem. 我有一个小问题。

I declare a class ITask. 我声明了一个ITask类。

文件任务模型中的ITask类

Then import it to component CreateTask. 然后将其导入component CreateTask。 Create a variable taskSend = new ITask(); 创建一个变量taskSend = new ITask();

在此处输入图片说明

And log it in ngOnInit(), it don't have some properties of class ITask. 并将其记录在ngOnInit()中, 它没有ITask类的某些属性。

In View have some error : CreateTaskComponent.html:9 ERROR TypeError: Cannot read property 'Name' of undefined 在视图中有一些错误: CreateTaskComponent.html:9 ERROR TypeError: Cannot read property 'Name' of undefined

在此处输入图片说明

Update: 更新:

File create-task.component.html 文件create-task.component.html

<div class="create-task-page container mt-4">
  <div class="d-block my-3">
    <h2 class="page-title my-3">{{ 'PAGES.TASK.HEADING.CREATE_TASK' | translate }}</h2>
  </div>
  <section class="mt-3">
    <form>
      <div class="form-group">
        <label for="task_name">{{ 'PAGES.TASK.LABEL.TASK_NAME' | translate }}</label>
        <input type="text" class="form-control" id="task_name" aria-describedby="taksHelp" [(ngModel)]="taskSend.task.Name" placeholder="{{ 'PAGES.TASK.PLACEHOLDER.TASK_NAME' | translate }}">
        <small id="taksHelp" class="form-text text-muted">{{ 'PAGES.TASK.HINT.TASK_NAME' | translate }}</small>
      </div>
      <div class="form-group">
        <label for="description">{{ 'PAGES.TASK.LABEL.DESCRIPTION' | translate }}</label>
        <td-text-editor value="content" [options]="options"></td-text-editor>
      </div>
      <div class="form-group">
        <label for="tags">{{ 'PAGES.TASK.LABEL.TAGS' | translate }}</label>
        <input type="text" class="form-control" id="tags" placeholder="{{ 'PAGES.TASK.PLACEHOLDER.TAGS' | translate }}" />
      </div>
      <button type="submit" class="btn btn-primary">{{ 'SYSTEM.ACTIONS.LABEL.SUBMIT' | translate }}</button>
    </form>
  </section>
</div>

read the error. 读取错误。 'Name' is a property from ITaskModel, and ITask has was a property of that type. “名称”是ITaskModel的属性,而ITask就是该类型的属性。 On ITask use the constructor to instantiate the other classes you also defined. 在ITask上,使用构造函数实例化您还定义的其他类。

https://www.typescriptlang.org/docs/handbook/classes.html https://www.typescriptlang.org/docs/handbook/classes.html

Your iTask class doesn't contain a property called Name . 您的iTask类不包含名为Name的属性。 But you are trying to access it in your html. 但是您正在尝试在html中访问它。 on line no. 在线号 9 9

[(ngModel)]="taskSend.task.Name"

This is the issue. 这就是问题。 To resolve it either add Name property to iTask or change the ngModel binded variable. 要解决该问题,可以将Name属性添加到iTask或更改ngModel绑定变量。

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

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