【问题标题】:controller not actualize string interpolation [duplicate]控制器未实现字符串插值[重复]
【发布时间】:2018-03-13 13:49:45
【问题描述】:

我有问题

我的 hello.component.html 是:

<form>
    <input ng-model="hello" type="text">
</form>
<p>{{hello}}</p>

hello.component.ts 是:

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

@Component({
  selector: 'app-hellow',
  templateUrl: './hellow.component.html',
  styleUrls: ['./hellow.component.css']
})
export class HellowComponent implements OnInit {

  hello:string ='';
  constructor() { }

  ngOnInit() {
  }

}

你能回答吗
如果我在输入中写了一些东西,控制器不会实现 {{hello}} 字符串插值。 你能帮我解决这个问题吗?

也尝试过:

<input [(ngModel)]="hello" type="text">

非常感谢。

【问题讨论】:

  • 检查您的控制台...
  • 尝试在输入框和[(ngModel)]上设置name属性为“hello”

标签: angular string-interpolation


【解决方案1】:

在标签中使用ngModel 时,您还需要提供一个名称属性,以便控件可以在该名称下注册到父窗体。 试试这个。

 <form>
        <input name="hello" [(ngModel)]="hello" type="text">
  </form>
    <p>{{hello}}</p>

【讨论】:

  • 行了!!谢谢
【解决方案2】:

你导入FormsModule了吗?

import { FormsModule } from '@angular/forms';

【讨论】:

  • 是的,我做到了。还在@NgModule 装饰器的导入数组中添加“FormsModule”
【解决方案3】:

问题解决了 我忘了在输入标签中添加name="hello" 属性
非常感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2013-05-06
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    相关资源
    最近更新 更多