如何为In Angular 4中的文本字段设置默认值?

时间:2018-04-04 06:23:04

标签: angular

我有一个for循环,我显示所选的产品名称和价格。用户可以选择输入数量,但默认情况下,数量应为1.如何设置文本字段值的默认值为1.

我尝试过以下方式,但它不起作用:

int month, day, year;
public String displayDate()
{
    return String.format ("%d/%d/%d", month,day, year);
}

System.out.printf("Date: %s%n", d.displayDate());  // change this as well

5 个答案:

答案 0 :(得分:2)

使用方括号中的value

[value]="myDefaultValue"
组件中的

myDefaultValue: number = 1;

或使用单向绑定:

<input type="text" (ngModelChange)="selectedProd[i].qty" name="qty" value="1">

答案 1 :(得分:0)

这些是你的选择

for(Dto dto : list){
    if(condition1(dto.getErrYn())){
        ...code...
    }else if(condition2(dto.getChangeYn())){
        ...code...
    }else if(condition3(dto.getRunYn())){
        ...code...
    }
}

答案 2 :(得分:0)

两种可能的解决方案:

  1. 首先尝试为绑定数据设置默认值。
  2. 使用占位符标记。

    working demo here

    import {Component, NgModule, VERSION} from '@angular/core'
    import {BrowserModule} from '@angular/platform-browser'
    import { FormsModule } from '@angular/forms'; 
    
    @Component({
      selector: 'my-app',
      template: `
        <div>
    
          <input type="text" [(ngModel)]="myValue" name="qty1">
          <input type="text" [(ngModel)]="myValue2" name="qty2" placeholder="1">
    
        </div>
      `,
    })
    export class App {
      myValue:string;
      myValue2:string;
    
      constructor() {
        this.myValue = "default value";
      }
    }
    
    @NgModule({
      imports: [ BrowserModule, FormsModule ],
      declarations: [ App ],
      bootstrap: [ App ]
    })
    export class AppModule {}
    
  3. 这是一个带有ngFor和bound属性占位符的working example

         <ng-container *ngFor="let item of items">
            <input type="text" [(ngModel)]="item.value" name="qty1"  [attr.placeholder]="price * qua" >
         </ng-container>
    ...
      items = [{value:null}, {value:"defVal"}];
    
      public qua = 3;
      public price = 7;
    

答案 3 :(得分:0)

试试这个

  

product.component.html

&#13;
&#13;
<tr *ngFor="let spro of selectedProd;let i = index;">
    <td data-title="Product Name">{{spro.product_description}}</td>
    <td data-title="Unit Price" class="numeric">
        <i class="fa fa-inr"></i>2000</td>
    <td data-title="Qty" class="numeric">
        <input type="text" [(ngModel)]="selectedQuantity" (ngModelChange)="quantityChanged($event, i)" name="qty">
    </td>
    <td data-title="Total Price" class="numeric">
        <i class="fa fa-inr"></i>{{selectedProd[i].qty*2000}}</td>
</tr>
&#13;
&#13;
&#13;

  

product.component.ts

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

@Component({
  moduleId: module.id,
  selector: 'app-product',
  templateUrl: 'product.component.html'
})
export class ProductComponent {
  public selectedProduct: any;
  public selectedQuantity: number = 1;

  constructor() {  }

  quantityChanged(e: any, index : any) {
     //event comes as parameter, you'll have to find selected data manually using event.target element
     this.selectedQuantity = e.target.qty;
     this.selectedProd[i].qty = this.selectedQuantity;
  }

}

答案 4 :(得分:0)

在appModule中导入 FormsModule 在html输入元素中使用([ngModel])<input type="text" [(ngModel)]="defaultQuantity"/>并在组件中创建变量名称 defaultQuantity:number = 1;