javascript - 在 angular2/typescript 中提交之前限制双向绑定(bind)

标签 javascript html angular

我有editPop up,我可以在弹出窗口中编辑表单字段。同时显示背景表格。在该表单中,我在 editPop 中输入的任何内容都会在提交之前以后台表单显示。如何在保存功能之前防止这种情况发生。

我需要仅在保存后显示它,而不是在弹出窗口中编辑时显示。

我猜这是因为 c.cName 发生了两种方式绑定(bind)。我该如何克服这个问题。

请帮忙。 我在这里分享我的 HTML 和 TS 代码。

HTML 代码

 <md-card class="col-lg-12 col-md-12 col-sm-12 col-xs-12" *ngFor="let c of cL ">
    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
      {{c.cName}}
    </div>
    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2" style="text-align:right;padding-right: 0px;">
   <button class="iconBtn" (click)="openC(c)">
 <md-icon svgIcon="edit" style="color: rgba(0,0,0,0.54);height: 17px;width: 17px;"></md-icon></button>
   </div>
   </md-card>

编辑按钮 HTML

<div class="modal-body row">
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                        <md-input-container style='width:100%'>
                            <input type="text" name="cName" mdInput [(ngModel)]="c.cName" placeholder="c" required>
                        </md-input-container>
                    </div>
  </div>

保存按钮 HTML

<div layout-align="end center" layout="row">
<button md-raised-button class="md-raised color-white" (click)="newC(c)" [disabled]="!cForm.form.valid" style="width: 46%;margin: 10px 5px;">Save</button>
                    </div>

Ts代码:

openC(c) {
 if(c) {
    this.msg = "edit";
    this.c = c;
    this.addC.show();
 } 
}
newC(c) {
  if(c._id) {    
    this.ApiService
         .editC(c,c._id)
         .subscribe(
           cs  => {
             this.toasterService.pop('success');
             this.addC.hide();
           },);
  } 
             this.ApiService
                 .getC()
                 .subscribe(
                   cs  => {
                     this.cs = cs.map(function(c) {
                        return {"value":c._id,"label":c.cName};
                      })
                     this.toasterService.pop('success');
                     this.cL = cs;
                    },
                   error => {
                     //console.log(error);
                   });
}

最佳答案

添加到上面的答案,你可以这样做

HTML

<md-input-container style='width:100%'>
   <input type="text" name="cName" 
    mdInput #updatedC [value]="c.cName"  placeholder="c" required>
 </md-input-container>

IN TS

update(updateC,c) {
c.cName:updateProject.value,
 this.ApiService
 .editc(C,c)
 .subscribe(
   c  => {
     this.toasterService.pop('success');
     this.editC.hide();
     this.ApiService
         .getC(this.c._id)
         .subscribe(
           c  =>{
             this.cL = c;
           },
           error => {
             //console.log(error);
           });

关于javascript - 在 angular2/typescript 中提交之前限制双向绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45098157/

相关文章:

java - 如何使用 Java 在 Selenium WebDriver 的隐藏字段中键入一些文本

javascript - URL 重写是否适用于 javascript window.open(url)?

javascript - 关于 "function invocation and the execution stack"中的示例

angular - 检测 Angular2 中数组内对象的变化

javascript - 无法访问 JSON 子元素

javascript - AngularJS:关于实例化服务

html - 不可见的 HTML 元素

javascript - 响应式导航栏下拉菜单(非 Bootstrap )

html - 如何在不使用不良做法的情况下内联 H2 和 HR 标签?

Angular 6 : ERROR Error: Cannot find control with unspecified name attribute with angular material and reactive forms