ng-bootstrap 单选按钮的 ngModel 绑定在添加 jQuery 脚本时在 Angular 2 中不起作用



当我的 Angular 2 应用程序中有以下代码时,绑定有效:

<!-- script src="node_modules/jquery/dist/jquery.min.js"></script-->
<script src="node_modules/tether/dist/js/tether.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>

但是浏览器中的控制台给出以下错误:

bootstrap.min.js:6 未捕获的错误:Bootstrap 的 JavaScript 需要 jQuery。 jQuery必须包含在Bootstrap的JavaScript之前。 at bootstrap.min.js:6

但是当我添加它(通过删除注释(时,绑定不起作用。

app.component.html:

<div [(ngModel)]="model" ngbRadioGroup name="radioBasic">
<label class="btn btn-primary">
<input type="radio" [value]="1"> Left (pre-checked)
</label>
<label class="btn btn-primary">
<input type="radio" value="middle"> Middle
</label>
<label class="btn btn-primary">
<input type="radio" [value]="false"> Right
</label>
</div>
<hr>
<pre>{{model}}</pre>

ng-bootstrap 项目的全部意义在于摆脱对 bootstrap.js jQuery 的依赖。这里的原因是,我们可以删除不需要的依赖项,并提供在 Angular 世界中更自然的 API。

因此,正如其他人评论的那样,不要在 Bootstrap 的 JavaScript 上添加依赖项(并在使用时跳过 jQuery(。请遵循ng-bootstrap的安装说明:https://ng-bootstrap.github.io/#/getting-started

你需要从 Bootstrap 获得的只是 CSS,不需要 JavaScript 部分。

最新更新