简体   繁体   English

如何使用Angular JS打开Bootstrap模式?

[英]How to open a Bootstrap modal with Angular JS?

I think what I really need to know is how to make changes to the documentation code that will allow me to implement my own modal. 我认为我真正需要知道的是如何对文档代码进行更改,以使我能够实现自己的模式。 It won't let me simply add a 它不会让我简单地添加一个

$scope.myTitle = "Title Here";

And then place it in the HTML, it needs to go through the modalInstance stuff? 然后将其放入HTML中,是否需要通过modalInstance进行处理?


I realize now that there is a specific way to open a Bootstrap modal using Angular JS, instead of how I been trying to do it using JQuery, but I cannot figure out how to implement this into my code? 我现在意识到,有一种使用Angular JS来打开Bootstrap模态的特定方法,而不是我一直尝试使用JQuery进行的方式,但是我不知道如何在我的代码中实现它?

I have tried 我努力了

$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')
        }
}

I get a bold line that appears at the top of the web page, as if a modal is trying to open. 我在网页顶部看到一条粗线,好像是一个模态试图打开一样。 Probably because it doesn't know what to open. 可能是因为它不知道要打开什么。 I am getting this code from https://angular-ui.github.io/bootstrap/ and I can't quite understand what opens the modal, like where it points to that specific modal. 我从https://angular-ui.github.io/bootstrap/获取此代码,但我不太了解是什么打开了模式,例如它指向该特定模式的位置。 I tried adding in the 我尝试添加

templateUrl: 'myModalContent.html', templateUrl:'myModalContent.html',

and wrapping the modal html in the script tags shown, but that doesn't do anything. 并将模态html包装在所示的脚本标签中,但不会执行任何操作。 Does there need to be a myModalContent.html? 是否需要一个myModalContent.html? I don't see this file existing in the Plunker for the documentation. 我没有在Plunker中找到该文件的文档。 What am I missing? 我想念什么?

Below is the old way I was attempting this. 以下是我尝试此操作的旧方法。 But it contains the base of my code before these new changes. 但是它包含了这些新更改之前的代码基础。

I read in this post Invoking modal window in AngularJS Bootstrap UI using JavaScript 我在这篇文章中阅读了使用JavaScript在AngularJS Bootstrap UI中调用模式窗口

about some CSS that needs to be added, which I've tried adding as well with no change. 有关一些需要添加的CSS的信息,我也尝试添加而没有任何更改。


So I've used Angular to fill a Bootstrap modal before with no problems. 因此,我之前曾使用Angular填充Bootstrap模态,没有任何问题。 But I think my specific situation this time around may be causing problems? 但是我认为这次我的具体情况可能会引起问题?

When I load the page with #s after URL, the modal does appear, but the Angular doesn't fill in as it shows the interpolates. 当我在URL后用#s加载页面时,模态确实出现,但是Angular并没有填写,因为它显示了插值。 In addition, I am getting this error: 另外,我收到此错误:

Error: p is undefined 错误:p未定义

Followed by the usual blob of CDN references to angular.min.js. 随后是对angular.min.js的通常CDN引用。 I don't even know where p is coming from. 我什至不知道p来自哪里。

Here is the code causing this: 这是导致此的代码:

if(typeof modalInfoTitle !== 'undefined'){
    $scope.modalTitle = modalInfoTitle;
    $scope.modalLinks = modalInfoLinks;

    urlExtract = location.hash
    foundExtract = urlExtract.split("#")[1];
    if(foundExtract == "s"){
        $('#waiverModal').modal('show');
    }
}

modalInfoTitle and modalInfoLinks are located in a separate JS file that is strictly JS. modalInfoTitlemodalInfoLinks位于严格的JS单独的JS文件中。 This file feeds my angular a bunch of other variables as well so I know that is not the problem. 这个文件也为我的角度提供了许多其他变量,所以我知道这不是问题。 Plus I can alert($scope.modalTitle); 另外我可以alert($scope.modalTitle); anywhere and it alerts the correct text. 任何地方,它都会提示正确的文本。

In other JS file: 在其他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"},
 ]];

The modal itself in the HTML file: 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">&times;</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>

So here is something different. 所以这里有些不同。 The error I showed earlier is the error I get using Firefox. 我之前显示的错误是我使用Firefox时遇到的错误。 However, here is the error I get with Chrome: 但是,这是我在Chrome中遇到的错误:

TypeError: Cannot read property 'childNodes' of undefined TypeError:无法读取未定义的属性“ childNodes”

 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) 

It will be much more effective to debug this issue if you put the related code into a plunkr. 如果将相关代码放入plunkr,则调试该问题将更加有效。

This probably would not throw the error you are seeing, but your conditional: 这可能不会引发您所看到的错误,但是您的条件是:

if(foundExtract = "s")

Should probably be using a comparison operator instead of an assignment operator: 应该使用比较运算符而不是赋值运算符:

if(foundExtract === "s")

Going back to how i was ORIGINALLY trying to accomplish this, when I was getting the Error: p is undefined (still don't know why p).. I FINALLY GOT THIS TO WORK. 回到我最初是如何尝试完成此操作的,当我遇到错误时:p是不确定的(仍然不知道为什么p)。我终于可以解决这个问题。

Despite the angular module that the angular code is in is in a ready function itself..all I needed to do was wrap the modal open code in a ready function and it works.... 尽管角度代码所在的角度模块本身在ready函数中..我要做的就是将模式开放代码包装在ready函数中并且可以工作....

    $scope.modalTitle = modalInfoTitle;
    $scope.modalLinks = modalInfoLinks;


   urlExtract = location.hash;
   foundExtract = urlExtract.split("#")[1];

   if(foundExtract == "s"){

    $(document).ready(function(){
        $('#waiverModal').modal('show');
    });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM