【问题标题】:How can I set input placeholder text in component file in Angular如何在 Angular 的组件文件中设置输入占位符文本
【发布时间】:2019-11-28 22:33:31
【问题描述】:

我是 Angular 和 Typescript 的新手。我在下面输入了placeholder,有没有办法从我的组件文件中更改placeholder 文本?

<input id="city" class="form-control" placeholder="City or Town">

【问题讨论】:

    标签: html angular typescript angular8


    【解决方案1】:

    您可以只绑定placeholder 属性。 Stackblitz

    <input id="city" class="form-control" [placeholder]="placeholder">
    
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      placeholder = 'Angular';
    }
    

    【讨论】:

    • 一旦我将占位符包装在 '[]' 中,我就会收到一条错误消息:“compiler.js:2430 Uncaught Error: Template parse errors: Parser Error: Unexpected token 'or' at column 6在 [City or Town in United States] in ng:///AppModule/RegisterComponent.html@110:24 ("Name="city" class="form-control google-place-input" [ERROR ->][placeholder ]="City or Town in United States">
    • 您能否编辑 Stackblitz 并展示您想要实现的目标?我尝试使用“美国的城市或城镇”,效果很好。
    • 我的错。我没有设置 placeholder = 'placeholder'
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签