添加 jQuery 脚本后,ng-bootstrap 单选按钮的 ngModel 绑定在 Angular 2 中不起作用
ngModel binding for ng-bootstrap radio button does not work in Angular 2 when jQuery script is added
当我的 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 Uncaught Error: Bootstrap's JavaScript requires jQuery. jQuery must be included before Bootstrap's 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 部分。
当我的 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 Uncaught Error: Bootstrap's JavaScript requires jQuery. jQuery must be included before Bootstrap's 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 部分。