这是我的模板代码摘录:

<ul class="panel-list bullet-list">
  <li *ngFor="let paragraph of selectedExperience.description">
    <div *ngIf="!editMode">
      {{ paragraph }}
    </div>
    <input
      *ngIf="editMode"
      {{paragraph}}
      id="paragraph"
      type="text"
      name="paragraph"
      placeholder="Párrafo"
    />
    <button
      (click)="deleteParagraph(selectedExperience, paragraph)"
      *ngIf="editMode"
      class="btn-edit"
    >
      <svg>
        ...
      </svg>
    </button>
  </li>
</ul>

我的意图是在启用编辑模式时将每个段落显示为输入,但我不知道如何做到这一点.我知道输入一定有问题.此外,我没有使用[(NgModel)],因为它不允许我使用,除非我执行以下操作:

[(ngModel)]="selectedExperience.description![0]"

当编辑模式=FALSE时,结果如下所示 enter image description here

这是(the problem)编辑模式=TRUE时的外观 enter image description here

推荐答案

最新情况:

here中派生出来的人,一定要给这个答案加分!不要更新变量,但使用索引更新数组属性!

<hello name="{{ name }}"></hello>
<p>Start editing to see some magic happen :)</p>
<input type="text" [value]="name" />
<li *ngFor="let paragraph of test; let i = index; trackBy: trackBy">
  <input type="text" [(ngModel)]="test[i]" />
</li>
{{ test | json }}

角码

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Angular';
  test = ['123', '456'];
  trackBy(index, item) {
    return index;
  }
}

stackblitz

这个怎么样?使用ngModel号装订并将段落设置为该装订.

<ul class="panel-list bullet-list">
  <li *ngFor="let paragraph of selectedExperience.description">
    <div *ngIf="!editMode">
      {{ paragraph }}
    </div>
    <input
      *ngIf="editMode"
      id="paragraph"
      [(ngModel)]="paragraph"
      type="text"
      name="paragraph"
      placeholder="Párrafo"
    />
    <button
      (click)="deleteParagraph(selectedExperience, paragraph)"
      *ngIf="editMode"
      class="btn-edit"
    >
      <svg>
        ...
      </svg>
    </button>
  </li>
</ul>

Angular相关问答推荐

导入浏览器模块时出错已导入commonModule的insted,animationBrower也显示错误,当p打开时

not getting circular input switch primeng组件

单元测试 case 模拟store 中的行为主体

[已解决]如何在模块ngDoBootstrap中测试Angular 自定义元素和做覆盖

如何在AngularJs中剪断细绳

如何将 Angular 中的导航路由到同一页面并重新加载

Angular ngSubmit 不起作用(内部按钮和导入模块)

手动关闭 SSE 连接:Angular

如何有条件地更新 Angular 路由解析器数据?

等待两个订阅完成而不嵌套

Angular:为什么在我的自定义 ErrorHandler 中订阅主题不触发?

显示 1 个数据而不是所有 ngFor - Angular 11

在一个组件中创建多个表单

带有重定向的Angular 2 AuthGuard服务?

将查询参数附加到 URL

类型错误:无法读取未定义的属性

Angular 没有可用于依赖类型的模块工厂:ContextElementDependency

如何在 Angular cli 项目中包含来自 node_modules 的assets

ngClass 中的多个条件 - Angular 4

服务相互依赖