数据绑定在角度4中不起作用

时间:2017-06-11 17:25:42

标签: angular

这可能太基本了,但我还没能弄明白。

我正在使用角度4.我在我的" app"中有一个文件夹。文件夹称为比赛。

这里我有我的competitions-detail.component.ts文件,如下所示:

import { Component }  from '@angular/core'

@Component({
  moduleId: module.id,
  selector: 'competitions-detail',
  templateUrl: 'competitions-detail.component.html',
  styleUrls: [ 'competitions-detail.component.css' ]
})

export class CompetitionsDetailComponent {
    title: 'Competencias';
}

然后我有我的模板文件:

<md-toolbar color="primary">
  <md-icon>data_usage</md-icon>
  <span><strong>LA M10</strong></span>
  <span class="spacer"></span>
  <span><strong>COMPETENCIAS</strong></span>
</md-toolbar>

<h1>COMPETENCIAS</h1>
<h1>{{title}}</h1>
<h2>COMPETENCIAS</h2>
<h2>{{title}}</h2>

这是Result

正如您所看到的,模板工作正常,如果我对它们显示的值进行硬编码,我甚至有一些材料设计组件,但由于某种原因,简单的数据绑定{{}}没有显示标题值

我错过了什么?

5 个答案:

答案 0 :(得分:3)

您正在分配类型,而不是实际设置字段:

title: string = 'Competencias';

您拥有它的方式,确保字段title只能包含字符串值:Compentencias

答案 1 :(得分:2)

类不是对象。它应该是=而不是:

export class CompetitionsDetailComponent{
    title = "Competencias";
 }

答案 2 :(得分:0)

尝试

export class CompetitionsDetailComponent {title =&#39; Competencias&#39 ;; }

而不是

export class CompetitionsDetailComponent {title:&#39; Competencias&#39 ;; }

答案 3 :(得分:0)

我认为变量赋值和声明存在问题。

title:string ='Competencias'

答案 4 :(得分:0)

变量赋值它应该是=而不是:

export class CompetitionsDetailComponent{
    title = "Competencias";
}
相关问题