【问题标题】:Angular 2: An attribute's component value does not show in the html (DOM)) when I looad the page for the first timeAngular 2:当我第一次加载页面时,属性的组件值不会显示在 html (DOM) 中
【发布时间】:2018-03-17 05:15:54
【问题描述】:

我在 Angular 2 中有一个如下所示的组件:

@Component({
  selector: 'gal-tour-summary',
  template: 'Show variable: {{this.testVariable}}', 
  styleUrls: ['./tour-summary.component.css']
})
export class TourSummaryComponent implements OnInit {

  cruise: any;
  itineraries: any;
  testVariable: string; //This is the variable i have created

  constructor(private utils: UtilsService) {
	testVariable = "Why this text does not appearr in my page when the component is loaded??"; //Here, in the constructo, I put some value to my variable, so, it should appear in my html now, right?
  }

  ngOnInit() {
    this.cruise = this.utils.linked['selectedBarco'];
    this.itineraries = this.createIterDay(this.cruise.itinerarios[this.cruise.iterIndex].tipoItinerario.detalleItinerarioList);
    console.log( 'Total de itinerarios: ' + this.itineraries.length );
  }
}

问题是我试图在 HTML 中显示 testVariable 的值,但这不起作用。我已经尝试了一切。这里可能是什么问题?你们之前有没有人遇到过同样的问题?

提前致谢!

【问题讨论】:

  • 错别字,testVariable应该是string
  • 删除“这个”。从变量(也是Queintin所说的),你应该发布你的错误信息
  • 感谢您的回答,伙计们! @q
  • 嗨,@QuentinLaillé。那个错字只出现在我在这里输入的示例中,但在我的代码中写得很好。对于那个很抱歉。我可以解决这个问题,它不在这个组件中,而是在另一个我可以在控制台中看到的错误中,我猜这导致其他组件无法正常工作。我是 Angular 的新手,所以我不知道另一个组件中的错误(在提出这个问题之前我已经看到过)可能会导致其他功能或组件无法正常工作。感谢您的回复。
  • 谢谢@Laurens!我可以解决这个问题,它不在这个组件中,而是在另一个我可以在控制台中看到的错误中,我猜这导致其他组件无法正常工作。我是 Angular 的新手,所以我不知道另一个组件中的错误可能会导致其他功能或组件无法正常工作。当您向我询问错误消息时,我告诉自己我应该首先解决该错误(在另一个组件中),以便我可以查看是否可以解决我的组件中的问题,就像这样。谢谢

标签: angular angular2-template angular-components


【解决方案1】:

我找到了解决方案。这听起来可能很愚蠢而且很明显,但我想这可能对刚接触 Angular 和 Web 开发的人有所帮助。

我的组件没问题!但是在 JS 控制台中,我可以看到另一个组件中存在错误。解决这个问题(在另一个组件中)我的应用程序开始正常工作。

我在这里学到的是,组件中的错误可能会使其他组件和功能无法正常工作。感谢那些回答问题的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2011-01-28
    • 2016-11-06
    • 1970-01-01
    相关资源
    最近更新 更多