【问题标题】:Angular 10 Modal Data Binding BootstrapAngular 10 模态数据绑定引导程序
【发布时间】:2020-08-22 10:30:03
【问题描述】:

我有一个理解问题,也许有人可以帮助我。 Angular 总是说@Input() 可以进入嵌套组件的深处,并通过@Output() 事件退出。我发现这是一个非常好的概念。那么为什么在我的案例中存在双向绑定。

感谢您的帮助!

app.component.ts:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: \['./app.component.scss'\]
})

export class AppComponent {

  public group: Group = {
    id: 1,
    name: 'Gruppe A',
    members: [
      {
        id: 1,
        firstName: 'Firstname A',
        lastName: 'Lastname A'
      },
      {
        id: 2,
        firstName: 'Firstname B',
        lastName: 'Lastname B'
      }
    ]
  };

  constructor(private ngbModal: NgbModal) {}

  public openModal(): void {
    const testModal = this.ngbModal.open(TestModalComponent);
    testModal.componentInstance.group = this.group;
    testModal.result.then((group) => {
      this.group = group;
    }).catch(() => {});
  }

}

`test-modal.component.ts`:

```typescript
@Component({
  selector: 'app-test-modal',
  templateUrl: './test-modal.component.html',
  styleUrls: ['./test-modal.component.scss']
})

export class TestModalComponent implements OnInit {

  @Input() group: Group;

  constructor(public ngbActiveModal: NgbActiveModal) {}

  public save() {
    this.ngbActiveModal.close(this.group);
  }

  public ngOnInit(): void {}

  ublic updateMember(member: Member): void {}
}

entry.component.ts:

@Input() member: Member;

@Output() updateMember: EventEmitter<Member> = new EventEmitter<Member>();

public memberForm = new FormGroup({
  firstName: new FormControl(null),
  lastName: new FormControl(null)
});

constructor() {}

public ngOnInit(): void {
  this.memberForm.patchValue({
    lastName: this.member.lastName,
    firstName: this.member.firstName
  });
}

public saveMember(): void {
  this.member.lastName = this.memberForm.getRawValue().lastName;
  this.member.firstName = this.memberForm.getRawValue().firstName;
}

entry.component.html

<form [formGroup]="memberForm">
  <div class="row mb-2">
    <div class="col-lg-5">
      <input
        class="form-control"
        formControlName="firstName"
        (ngModelChange)="saveMember()"
      />
    </div>
    <div class="col-lg-5">
      <input
        class="form-control"
        formControlName="lastName"
        (ngModelChange)="saveMember()"
      />
    </div>
    <div class="col-lg-2">
      <button class="btn btn-success">
        <i class="fas fa-save"></i>
      </button>
    </div>
  </div>
</form>

【问题讨论】:

    标签: angular bootstrap-modal two-way-binding


    【解决方案1】:

    如果你的意思是 entry.component.ts 成员和成员更新,你可以这样做

    @Input() member: Member;
    
    @Output() memberChange: EventEmitter<Member> = new EventEmitter<Member>();
    

    使用示例

    <app-entry [(member)]="member"></app-entry>
    

    在这种情况下,您可以只使用[(member)]="member"

    请注意:要使用香蕉盒([()]),您需要输入@Input() property和输出@Output() propertyChange = new EventEmitter&lt;Member&gt;();方法。

    【讨论】:

    • 您好,非常感谢您的回答。我的意思是它是双向绑定的,尽管我没有输出事件,也没有香蕉盒,这就是问题所在。我希望 @Input() 不会自动将值播放到父组件中,只有在触发 @Output() 事件时。
    • 对不起,我没有得到这部分I would like @Input() to not automatically play the value into the parent component, only when the @Output() event is triggered
    猜你喜欢
    • 2020-01-30
    • 1970-01-01
    • 2016-06-24
    • 2014-07-28
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多