简体   繁体   English

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

[英]data binding is not working in angular 4

This is probably too basic but I haven't been able to figure it out. 这可能太基础了,但是我还没弄清楚。

I'm working with angular 4. I have a folder within my "app" folder called competitions. 我正在使用angular4。在“ app”文件夹中有一个名为Competitions的文件夹。

Here I have my competitions-detail.component.ts file like this: 在这里,我有如下的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';
}

Then I have my template file: 然后我有我的模板文件:

<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>

And this is the Result 这就是结果

As you can see, the template is working fine, if I hardcode the values they are displayed, I even have some material design components there but for some reason, the simple data binding {{ }} is not displaying the title value. 如您所见,该模板运行良好,如果我对显示的值进行硬编码,甚至还有一些材料设计组件,但是由于某种原因,简单的数据绑定{{}}不会显示标题值。

What am I missing? 我想念什么?

You are assigning a type, and not actually setting the field: 您正在分配类型,而不是实际设置字段:

title: string = 'Competencias';

The way you had it, made sure the field title could only have the string value: Compentencias 使用方式,请确保字段title只能具有字符串值: Compentencias

Class is not an object . 类不是对象。 It should be = instead of : 应该是=而不是:

export class CompetitionsDetailComponent{
    title = "Competencias";
 }

Try 尝试

export class CompetitionsDetailComponent { title = 'Competencias'; 出口类CompetitionsDetailComponent {title ='Competencias'; } }

Instead of 代替

export class CompetitionsDetailComponent { title: 'Competencias'; 出口类CompetitionsDetailComponent {标题:'Competencias'; } }

I think variable assignment and declaration have problem. 我认为变量赋值和声明有问题。

title: string = 'Competencias' title:字符串='Competencias'

The variable assignment It should be = instead of : 变量赋值应为=而不是:

export class CompetitionsDetailComponent{
    title = "Competencias";
}

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

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