【问题标题】:how to add the placeholder value to ngmodel如何将占位符值添加到 ngmodel
【发布时间】:2020-05-28 14:53:08
【问题描述】:

我正在关注primeng 中的一个示例,我可以在其中向表中添加新行。只要我填写输入选项中的所有字段,一切“似乎”都可以工作。但是,如果用户不更改输入的值,我想将占位符值添加到 NGmodel。我尝试了所有方法(ng-init、ngvalue 等),但我永远无法让 ngmodel 在占位符中携带值......并且表格填充了 3 个填充字段,但不是用户没有填充的字段键入任何内容。 HTML 的提取....

<div class="ui-g-12">
        <div class="ui-g-4">
            <label for="product_line_id">Product ID</label>
        </div>
        <div class="ui-g-8">
            <input pInputText id="product_line_id"  [ngModel]="myproduct.product_line_id"  placeholder="{{ lastproductline + 1}}" />
        </div>
    </div>
    <div class="ui-g-12">
        <div class="ui-g-4">
            <label for="product_line_1">Product</label>
        </div>
        <div class="ui-g-8">
            <input pInputText id="product_line_1" [(ngModel)]="myproduct.product_line_1" autofocus />
        </div>
    </div>
    <div class="ui-g-12">
        <div class="ui-g-4">
            <label for="product_line_2">Category</label>
        </div>
        <div class="ui-g-8">
            <input pInputText id="product_line_2" [(ngModel)]="myproduct.product_line_2" />
        </div>
    </div>
    <div class="ui-g-12">
        <div class="ui-g-4">
            <label for="product_line_3">Sub Category</label>
        </div>
        <div class="ui-g-8">
            <input pInputText id="product_line_3" [(ngModel)]="myproduct.product_line_3" />
        </div>
    </div>

the ts file looks something like...

productlines = [];
myproduct: { [s: string]: ProductLines; } = {};
showDialogToAdd() {
    this.newProductLine = true;
    this.myproduct = {}; 
    this.displayDialog = true;
  }

  save() {
    let productlines = [...this.productlines];
    productlines.push(this.myproduct);
    this.finalproductchanges.push(this.myproduct)
    this.productlinesClone = productlines;
    this.myproduct = null;
    this.displayDialog = false;
  }

Any ideas will be greatly appreciated

【问题讨论】:

  • 您是否尝试将值设置为 ngModels 中的变量以将值保留在输入中?或者简单地在你的 html 中添加一个 placeholder="mytext" ?

标签: placeholder angular9 ngmodel


【解决方案1】:

我设法通过直接在控制器中添加字段来更改代码。

【讨论】:

    猜你喜欢
    • 2020-08-10
    • 2016-08-22
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 2020-11-05
    相关资源
    最近更新 更多