如何用Angular JS打开一个Bootstrap模态
How to open a Bootstrap modal with Angular JS?
我认为我真正需要知道的是如何更改文档代码,这将允许我实现自己的模态。它不允许我简单地添加
$scope.myTitle = "Title Here";
然后把它放在HTML中,它需要通过modalInstance的东西?
我现在意识到,有一个特定的方式来打开一个Bootstrap模式使用Angular JS,而不是我一直试图使用JQuery,但我不知道如何实现这到我的代码?
我试过了
$scope.modalTitle = modalInfoTitle;
$scope.modalLinks = modalInfoLinks;
$scope.open = function (size) {
var modalInstance = $modal.open({
size: size,
});
urlExtract = location.hash
foundExtract = urlExtract.split("#")[1];
if(foundExtract == "s"){
$scope.open('lg')
}
}
我得到一个粗体线,出现在网页的顶部,如果一个模态试图打开。可能是因为它不知道该打开什么。我从https://angular-ui.github.io/bootstrap/得到这个代码,我不能完全理解是什么打开模态,就像它指向那个特定的模态。我试着添加
templateUrl:"myModalContent.html",
并将模态HTML包装在所示的脚本标签中,但这没有做任何事情。是否需要一个mymodalcontent。html?我没有看到这个文件存在于普伦克的文件。我错过了什么?
下面是我尝试的旧方法。但它包含了这些新更改之前我的代码的基础。
我在这篇文章中读到使用JavaScript在AngularJS Bootstrap UI中调用模态窗口
关于一些需要添加的CSS,我也尝试过添加,但没有改变。
所以我以前用Angular填充过Bootstrap模态,没有任何问题。但我想这次我的特殊情况可能会造成问题?
当我在URL后加载#s
页面时,模态确实出现了,但Angular没有填充,因为它显示了插值。另外,我得到这个错误:
错误:p is undefined
后面是对angular.min.js的CDN引用。我甚至不知道p是从哪里来的
下面是导致这个的代码:
if(typeof modalInfoTitle !== 'undefined'){
$scope.modalTitle = modalInfoTitle;
$scope.modalLinks = modalInfoLinks;
urlExtract = location.hash
foundExtract = urlExtract.split("#")[1];
if(foundExtract == "s"){
$('#waiverModal').modal('show');
}
}
modalInfoTitle
和modalInfoLinks
位于一个单独的JS文件,严格是JS。这个文件为我的角提供了一堆其他变量,所以我知道这不是问题。另外,我可以在任何地方alert($scope.modalTitle);
,它提醒正确的文本。
在其他JS文件:
var modalInfoTitle = "Modal Title";
var modalInfoLinks = [
[
{title:"Name1",link:"file1.pdf"},
{title:"Name3",link:"file3.pdf"},
],[
{title:"Name2",link:"file2.pdf"},
{title:"Name4",link:"file4.pdf"},
]];
模态本身在HTML文件中:
<div class="modal fade buildingsModal" id="waiverModal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">{{modalTitle}}</h4>
</div>
<div class="modal-body">
<table id="tableInModal">
<tr ng-repeat="row in modalLinks">
<td ng-repeat="z in row"><a href="{{z.link}}">{{z.title}}</a></td>
</tr></table>
</div>
<div class="modal-footer clear">
</div>
</div>
</div>
</div>
这里有些不同。前面显示的错误是我使用Firefox时出现的错误。然而,这是我在Chrome上得到的错误:
TypeError: Cannot read property 'childNodes' of undefined
at g (angular.min.js:47) at g (angular.min.js:47) at g (angular.min.js:47) at g (angular.min.js:47) at g (angular.min.js:47) at g (angular.min.js:47) at J (angular.min.js:54) at g (angular.min.js:47) at g (angular.min.js:47) at g (angular.min.js:47)
如果您将相关代码放入plunkr中,则调试此问题将更加有效。
这可能不会抛出您所看到的错误,但是您的条件:
if(foundExtract = "s")
应该使用比较操作符而不是赋值操作符:
if(foundExtract === "s")
回到我最初试图实现这一点,当我得到错误:p是未定义的(仍然不知道为什么p)。我终于把它弄好了。
尽管angular代码所在的angular模块本身是在一个ready函数中,但我所需要做的就是将模态打开代码包装在ready函数中,它就可以工作了....
$scope.modalTitle = modalInfoTitle;
$scope.modalLinks = modalInfoLinks;
urlExtract = location.hash;
foundExtract = urlExtract.split("#")[1];
if(foundExtract == "s"){
$(document).ready(function(){
$('#waiverModal').modal('show');
});
}
- bootstrap消除模态并显示另一个模态
- 在Twitter BootStrap嵌套下拉列表中托管一个选择列表(IE 9特定)
- 在Bootstrap+Angular中一个接一个地显示警报
- Bootstrap折叠手风琴一个面板打开,所有其他面板关闭
- 如何创建一个“下一步”按钮,使用 Java 脚本和 Twitter Bootstrap 切换选项卡
- Bootstrap下拉菜单中的复选框允许多个复选框而不是一个
- Bootstrap如何知道一个元素是否有popover
- 导航到另一个页面后,bootstrap下拉列表将不可用
- 包括bootstrap.min.css和bootstrap.css,或者只包括一个
- 创建一个复选框按钮的散列,这些按钮在单击时在Twitter Bootstrap Button组中处于活动状态
- Twitter Bootstrap Popover添加了一个带有事件的回调函数
- 如何使一个Bootstrap 3下拉菜单默认打开时,它是在一个折叠的导航条
- 如何用Angular JS打开一个Bootstrap模态
- 如何检查一个Bootstrap UI模态是否打开?——AngularJS
- 是否有一个Bootstrap类的东西只在移动设备上消失
- 为google maps API v3标记添加一个bootstrap 3工具提示
- 用javascript获取一个bootstrap标签的id
- 淡入淡出一个Bootstrap 3模式
- 打开一个Bootstrap和Angularjs不工作的盒子
- 创建一个Bootstrap弹出窗口模式