美文网首页
AppComponent.html:7 ERROR DOMExc

AppComponent.html:7 ERROR DOMExc

作者: 游海东 | 来源:发表于2019-12-03 17:14 被阅读0次

1. 错误描述

错误描述

2. 错误原因

由于Angular在绑定元素属性时,不能出现单个括号,即是语法错误导致的。

app.component.ts

import { Component } from '@angular/core';
import { Student } from '../student/student';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  template: `
    <h1>{{title}}</h1>
    <img [src]="imgUrl"]>
    <h2>名称:{{one.name}},年龄:{{one.sage}}</h2>
    <p *ngIf="student.length > 4">超过四个</p>
    <ol>
    <li *ngFor="let stu of student">
      {{ stu.name }}
    </li>
  </ol>`,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'amk';
  imgUrl = '';
  student = [
    new Student(1, 'zs', 20),
    new Student(2, 'ls', 21),
    new Student(3, 'su', 21),
    new Student(4, 'zhu', 21),
    new Student(5, 'lei', 21)
  ];

  one = this.student[0];
}


student.ts

export class Student {
  sno: number;
  name: string;
  sage: number;

  constructor(sno: number, name: string, sage: number) {
    this.sno = sno;
    this.name = name;
    this.sage = sage;
  }
}

3. 解决办法

去掉多余的符号

import { Component } from '@angular/core';
import { Student } from '../student/student';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  template: `
    <h1>{{title}}</h1>
    <img [src]="imgUrl">
    <h2>名称:{{one.name}},年龄:{{one.sage}}</h2>
    <p *ngIf="student.length > 4">超过四个</p>
    <ol>
    <li *ngFor="let stu of student">
      {{ stu.name }}
    </li>
  </ol>`,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'amk';
  imgUrl = '';
  student = [
    new Student(1, 'zs', 20),
    new Student(2, 'ls', 21),
    new Student(3, 'su', 21),
    new Student(4, 'zhu', 21),
    new Student(5, 'lei', 21)
  ];

  one = this.student[0];
}

相关文章

网友评论

      本文标题:AppComponent.html:7 ERROR DOMExc

      本文链接:https://www.haomeiwen.com/subject/zrfhgctx.html