简体   繁体   English

angular2如何调用对象的方法

[英]angular2 how to call method of object

I have an model in which I have a method is valid 我有一个方法有效的模型

export class ModelClass{
   id: number;
   text : String,

   isValid() : boolean {
      return true;
}
}

a second model 第二种模式

export class Rows{
   editable: boolean;
   modelClass: ModelClass;
}

how to call it in html ??? 如何在html中调用它?

<table border="1p;">

  <tbody >
    <tr *ngFor="let link of modelClassRows">
      <td>
         <div>toto {{link.applicationLink.isValid()}}</div>
      </td>
   </tr>

  </tbody>
</table>

a part of my component where object are initialized 我组件的一部分,对象已初始化

export class ApplicationLinkListComponent implements OnInit {

modelClassRows: Rows[] = new Array<Rows>();

constructor() {
  }

ngOnInit() {
   // fill modelClassRows
.....
}

You need to instantiate the ModelClass class this way: 您需要通过以下方式实例化ModelClass类:

let row = new Rows();
row.modelClass = new ModelClass();
row.modelClass.id = 'some id';
row.modelClass.title = 'some title';
modelClassRows.push(row);

If you instantiate it literally and cast it to the type, you won't be able to use the method. 如果您将其实例化并转换为类型,则将无法使用该方法。

For example: 例如:

let row = new Rows();
row.modelClass = <ModelClass>{
  id: 'some id',
  title: 'some title'
};
modelClassRows.push(row);

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

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