javascript - Modal 中的两种方式绑定(bind)变量 - Angular

标签 javascript jquery angular

我有一个 Bootstrap Modal,我使用 jQuery 初始化它,如下所示:

$('#saleModal').modal('show'); // I know I should use the angular way in initializing the modal but...

Modal中,我有一个输入,我需要将其绑定(bind)到变量,如下所示:

<input type="text" [(ngModel)]="toBeBoundVariable">

但是绑定(bind)不起作用。我如何告诉 Angular 检查该 Modal 中的绑定(bind)。

我对它没有绑定(bind)的原因的最佳猜测是因为 Modal 是使用 jQuery 初始化的。但是,有没有办法告诉 Angular,“嘿,有一个 Bootstrap Modal 已初始化,有一些您需要注意的绑定(bind)”。

这是代码:

<!--Modal start-->
 <div class="modal" id="saleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel_4"
 aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel_4">New/Edit Sale</h5>
         <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true"><i class="ion-ios-close-empty"></i></span>
        </button>
      </div>
  <form>

    <div class="modal-body">

      <label class="col-sm-4 form-control-label">Name</label>
      <div class="col-sm-8 mg-t-10 mg-sm-t-0">
        <input type="text" class="form-control" [(ngModel)]="firstname">
      </div>
      Bound firstname: {{firstname}}
    </div>

    <div class="modal-footer">
      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      <button type="submit" class="btn btn-primary"
      >
        <i></i>Save
      </button>
    </div>
  </form>
</div>

.ts 文件

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

declare var $: any;

@Component({
  selector: 'app-purchase',
  templateUrl: './purchase.component.html',
  styleUrls: ['./purchase.component.css']
})
export class PurchaseComponent implements OnInit {

  firstname: string;

  constructor() {


  }

  ngOnInit() {

  }


  showModal() {
   $('#saleModal').modal('show');
  }


}

最佳答案

我认为您错过了在输入字段中添加 name 属性。

  <input type="text" class="form-control" [(ngModel)]="firstname" name="firstName">

希望它能解决您的问题。确保您已在模块中导入 FormsModule。

关于javascript - Modal 中的两种方式绑定(bind)变量 - Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58820235/

相关文章:

javascript - jQuery:隐藏按钮直到选择最后一个下拉列表

javascript - 如何按值在下拉列表中选择 <option> 并为其分配 "selected"属性?

html - Angular 2 应用程序中只有两个 css 文件中的一个有效

angular - 从 HTML 中删除/替换组件的选择器标签

javascript - 如何在不使用路径元素坐标的情况下在 React 中的 SVG 路径元素上叠加文本?

javascript - 如何在 Javascript 中为表格中的每个单元格 Hook onclick 事件?

javascript - 如何禁用在特定 DataTable 列中的搜索?

html - Angular 中的可滚动路由器导出

javascript - PHP : Is it possible to include a file from a sibling directory?

javascript - Chrome 扩展程序内容脚本未加载