【问题标题】:Angular 5 : ngModel inside ng-container does not showAngular 5:ng-container 中的 ngModel 不显示
【发布时间】:2018-07-21 11:17:07
【问题描述】:

在一个视图中,我以这种方式显示另一个组件:

<app-matrix-unit *ngIf="isSelectedUnit" [unit]="selectedUnit" [awards]="firstLevelAwards"></app-matrix-unit>

在 app-matrix-unit 组件中,我有一个表单,其中包含以这种方式初始化的 selectedUnit 的多个字段:

    export class MatrixUnitComponent implements OnInit {


  @Input('unit')
  private unit: any;

  @Input('awards')
  private awards: any[];

  private types: any[] = ['REMISE', 'SUR_REMISE', 'COMMISSION', 'COMMISSION_ADDITIONNELLE'];

  private owners: any[] = [{key: "OWN", value: "Propre"}, {key: "TEAM", value: "Equipe"}];

  constructor() { }

  ngOnInit() {
  }

}

这是视图:

<form name="unitForm">
        <fieldset>
            <div class="form-group">
                <label for="name">Type :</label>
                <select [(ngModel)]="unit.type" name="tye" class="custom-select">
                    <option *ngFor="let type of types" [selected]="unit.type == type" [ngValue]="type">{{type | camelCase}}</option>
                </select>           
            </div>
            <div class="form-group">
                <label for="firstName">Attribuée sur le CA généré par :</label>
                <select [(ngModel)]="unit.owner" name="owner" class="custom-select">
                    <option *ngFor="let owner of owners" [selected]="unit.owner == owner.key" [ngValue]="owner.key">{{owner.value | camelCase}}</option>
                </select>       
            </div>
            <div class="form-group">
                <label for="to">Attribué sur le CA au niveau :</label>
                <select [(ngModel)]="unit.to" name="to" class="custom-select">
                    <option *ngFor="let award of awards" [selected]="award == unit.to" [ngValue]="award">{{award.name | camelCase}}</option>
                </select>   
            </div>
            <div class="form-group">
                <label for="to">Attribué au niveau :</label>
                <select [(ngModel)]="unit.afor" name="afor" class="custom-select">
                    <option *ngFor="let award of awards" [selected]="award == unit.afor" [ngValue]="award">{{award.name | camelCase}}</option>
                </select>   
            </div>
            <div class="form-group">
                <label for="limit">Limite de CA :</label>
                <div class="row">
                    <div class="col-sm-2">
                        <input type="number" class="form-control small-input" name="limit" aria-describedby="limit-addon" [(ngModel)]="unit.limit" value="{{unit.limit}}">
                    </div> 
                    <div class="col-sm-2">€</div>
                </div>
            </div>

            <div class="form-group">
                <label for="limit">% de commission :</label>
                <div class="row"><div class="col-sm-2">
                    <input type="text" class="form-control small-input" name="limit" [(ngModel)]="unit.percentage" value="{{unit.percentage}}">
                </div>
                <div class="col-sm-2">%</div></div>
            </div> 


            <div class="form-group">
                <label for="limit">Eligible seul ?</label>
                <input type="checkbox" class="form-control" name="limit" [(ngModel)]="unit.alone">
            </div>


        </fieldset>
    </form>

有些字段渲染得很好,但例如,ngModel = unit.percentage 的字段显示输入为“false”。

在 Chrome 调试器中,这里是元素:

<input _ngcontent-c5="" class="form-control small-input ng-untouched ng-pristine ng-valid" name="limit" type="text" ng-reflect-name="limit" **ng-reflect-model="8"**>

我不明白为什么它显示出如此不连贯的价值。我还尝试从 API 重新加载对象,而不是通过组件声明传递它,但结果相同。

【问题讨论】:

    标签: angular angular5


    【解决方案1】:

    好的,不能正常工作的字段具有错误的“名称”属性。每个领域都有好的,效果很好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2019-09-13
      • 2021-09-06
      • 1970-01-01
      • 2018-09-29
      • 2018-02-18
      • 2018-09-28
      相关资源
      最近更新 更多