组件HTML中的Angular 2调用函数返回空

时间:2017-03-20 13:51:31

标签: javascript angular typescript

这是我的ts文件。当我在另一个或相同的打字稿中调用tempData()方法时,它将返回正确的信息。但是当我从HTML文件中调用它时,它返回0 this.links.length ,并且还返回错误的ID this.graph

export class EditRelationColsComponent implements OnInit {
  graph: any = new joint.dia.Graph;


constructor(private globalService: GlobalService) {
  }

ngOnInit() {
 }
  tempData(){
    console.log(this.graph);
    console.log(this.links.length);
 }
}

HTML

<button class="diag" pButton type="button" (click)="tempData()"  class="ui-button-info" label="Save"></button>

所以当我从另一个JS调用函数时(在我调用 loadGraph()之后),我的图形ID是:307 当我从HTMl调用函数时,我的图形ID是:308。我不知道为什么它不是在寻找合适的图形,而是创建新的图形?

1 个答案:

答案 0 :(得分:0)

关闭标签太快&#34;}&#34;。试试这个。

export class EditRelationColsComponent implements OnInit {
  graph: any = new joint.dia.Graph;


  constructor(private globalService: GlobalService) {
  }

 ngOnInit() {
 }
 tempData(){
    console.log(this.graph);
    console.log(this.links.length);
 }
}