小编Mav*_*vil的帖子

ngModel在Angular4中不起作用

我正在从官方网站学习Angular 4 ,我通过ngModel来到双向数据绑定的部分.但是,一旦我将[(ngModel)]添加到我的组件模板,我的应用程序就会停止工作,即使在module.ts文件中导入了FormsModule.组件无法加载.
我正在使用Visual Studio Code.
这是我的app.component.ts

import { Component } from '@angular/core';

    export class Hero {
      id: number;
      name: string;
    }



    @Component({
      selector: 'app',
      template: `
      <h1>{{ title }}</h1>
      <h2>{{hero.name}} details!</h2>
      <div><label>Id:</label> {{hero.id}} </div>
      <div>name:<input [(ngModel)]="hero.name" type="text"></div>
      `,
      styles:[`
        .selected{
          transition: padding 0.3s;
          color: mediumseagreen;
        }
      `]
    })

    export class AppComponent {
      title = 'Tour Of Heroes';

     hero:Hero = {
       id:1,
       name:"Mr. Invisible"
     };
    }  
Run Code Online (Sandbox Code Playgroud)

这是app.module.ts

import { FormsModule } from …
Run Code Online (Sandbox Code Playgroud)

ngmodel angular

10
推荐指数
3
解决办法
2万
查看次数

标签 统计

angular ×1

ngmodel ×1