我有一个像这样的KnowledgeElement模型:

export interface KnowledgeElement {
  public knowledgE_USER_ID: number;
  public knowledgE_NAME: string;
  public knowledgE_DESCRIPTION: string;
  public knowledgE_USER_SCORE: number;
}


和这样的组件:

@Component({
  selector: 'survey',
  moduleId: module.id,
  styleUrls: ['survey.component.css'],
  templateUrl: 'survey.component.html',
  providers: [KnowledgeElementDataService]
})

export class SurveyComponent implements OnInit {
  public knowledgeElement: KnowledgeElement = {};

  constructor(private _knowledgeList: KnowledgeElementDataService){

  }

  submitSurvey(): void {
    this.knowledgeElement.knowledgE_USER_ID = 1;
    this.knowledgeElement.knowledgE_NAME = 'Empty';
    this.knowledgeElement.knowledgE_DESCRIPTION = 'Empty';
    this.knowledgeElement.knowledgE_USER_SCORE = 1;

    this._knowledgeList.updateKnowledge(knowledgeElement);
  }
}


当我尝试将属性分配给变量时,其说明如下:
“原因:无法设置未定义的属性'knowledgE_USER_ID'”

如何为该变量分配属性,以便将其与方法一起传递给服务?

最佳答案

this._knowledgeList.updateKnowledge(knowledgeElement);


应该

this._knowledgeList.updateKnowledge(this.knowledgeElement);


如果您有“错别字”,则应该像本地变量一样传递this.knowledgeElement而不是knowledgeElement :-)

07-24 09:54