【问题标题】:input box value not printed输入框值未打印
【发布时间】:2017-09-03 22:58:36
【问题描述】:

代码:

<div class="row" *ngIf="goalStatusList;" >
  <table class="table table-hover">
    <thead>
    <tr>
      <td>Id</td>
      <td>Name</td>
    </tr>
    </thead>
    <tbody>
    <tr *ngFor="let goalStatus of goalStatusList">
      <th>{{goalStatus.goalStatusId}}</th>
      <td><div *ngIf="!(goalStatus.editorEnabled)">
{{goalStatus.goalStatusName}}
<md-icon (click)="goalStatus.editorEnabled=true;">mode_edit</md-icon></div>
  <div *ngIf="(goalStatus.editorEnabled)" ><md-input-container><input mdInput value="goalStatus.goalStatusName" #goalName></md-input-container><md-icon (click)="modifyGoal()" style="color:green;font:bold;" >done</md-icon><md-icon (click)="goalStatus.editorEnabled=false" style="color:red;font:bold;" >clear</md-icon></div></td>
    </tr>
    </tbody>
  </table>
</div>

上面的 mdInput 字段不打印goalStatusName 值。相反,它只是打印goalStatus.goalStatusName

【问题讨论】:

    标签: angular angular-material2


    【解决方案1】:

    这里有两个问题

    你的 ngIf 应该没有括号,

    <td><div *ngIf="!goalStatus.editorEnabled">
    

    如果有值则使用表达式

    <input mdInput value="{{goalStatus.goalStatusName}}" #goalName>
    

    【讨论】:

    • 这里如何实现双向绑定?
    • 你应该使用 ngModel
    • 试过了。出现错误:Can't bind to 'ng-model' since it isn't a known property of 'input'. ("dit&lt;/md-icon&gt;&lt;/div&gt;。我的代码:&lt;md-input-container&gt;&lt;input mdInput ng-model="{{row.goalStatusName}}" #goalName&gt;&lt;/md-input-container&gt;
    • 已解决:&lt;input mdInput [(ngModel)]="row.goalStatusName" #goalName&gt;
    猜你喜欢
    • 2021-07-25
    • 2021-02-21
    • 2022-06-12
    • 2019-06-04
    • 1970-01-01
    • 2021-08-13
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多