如何使用.innerHTML插入包含AngularJS的Bootstrap模式,然后手动打开它?

时间:2015-06-02 18:47:40

标签: jquery angularjs twitter-bootstrap

当我的页面加载时,这就是我想要它做的事情:

$(function () {
    urlExtract = location.hash
    foundExtract = urlExtract.split("#/")[1];
    if(foundExtract == "zsexdr"){
        var createModal = "";

        basicRep = document.getElementById('basicReports');

        createModal+='<div class="modal fade" id="#waiverModal" role="dialog">';
        createModal+='<div class="modal-dialog modal-sm">';
        createModal+='<div class="modal-content">';
        createModal+='<div class="modal-header">';
        createModal+='<button type="button" class="close" data-dismiss="modal">&times;</button>';
        createModal+='<h4 class="modal-title">{{z.name}} - Downloads</h4>';
        createModal+='</div>';
        createModal+='<div class="modal-body">';
        createModal+='<span ng-repeat="z in waiverModalLinks" class="{{z.id}}"><a href="{{z.link}}{{z.title}}">{{z.title}}</a></span>';
        createModal+='</div>';
        createModal+='<div class="modal-footer">';
        createModal+='</div>';
        createModal+='</div>';
        createModal+='</div>';
        createModal+='</div>';

        basicRep.innerHTML+=createModal;

        $('#waiverModal').modal('show');
    }
})

模态被创建并插入到HTML中就好了,除了AngularJS没有注册,它注册了同一个div id="basicReports">中我正在使用ui.bootstrap的其他角度我有让AngularJS插入到不同页面上的模态中,但这是我第一次尝试动态插入模态并以编程方式打开它。所以我不确定我是否正确地做到了。

我的主要问题是,随着Angular没有注册,模态没有打开。我知道模态是存在的,因为如果我检查元素我可以在HTML中看到它

2 个答案:

答案 0 :(得分:1)

您必须使用$compile for angular来解析html。

模态未打开,因为在html中您已将模态ID指定为#waiverModal而不是waiverModal。此外,由于页面上已经有jQuery,因此可以使用jQuery方法进行DOM操作。

(function(){
var app = angular.module('AVS', ['ui.bootstrap']);

app.controller('BasicInformation', function($scope, $compile){

   $scope.basicreports = [
   {name: 'Online Edit', link: 'url', status:''},
   {name: 'Project Log', link: 'url', status:''}

   ];

   $scope.waiverModalLinks =[{
      title:"Title",
      link:"Links",
      color:"color?"
   }];


    var urlExtract = location.hash,
    foundExtract = urlExtract.split("#/")[1];
    if(foundExtract == "zsexdr"){
        var createModal = "", 
        basicRep = $('#basicReports');

        createModal+='<div class="modal fade" id="waiverModal" role="dialog">';
        createModal+='<div class="modal-dialog modal-sm">';
        createModal+='<div class="modal-content">';
        createModal+='<div class="modal-header">';
        createModal+='<button type="button" class="close" data-dismiss="modal">&times;</button>';
        createModal+='<h4 class="modal-title">{{z.name}} - Downloads</h4>';
        createModal+='</div>';
        createModal+='<div class="modal-body">';
        createModal+='<span ng-repeat="z in waiverModalLinks" class="{{z.id}}"><a href="{{z.link}}{{z.title}}">{{z.title}}</a></span>';
        createModal+='</div>';
        createModal+='<div class="modal-footer">';
        createModal+='</div>';
        createModal+='</div>';
        createModal+='</div>';
        createModal+='</div>';


        createModal = $compile(createModal)($scope);


        basicRep.append(createModal);

        $('#waiverModal').modal('show');
    }

 });

})();

答案 1 :(得分:1)

如果您有兴趣进一步了解&#34; AngularJS Way&#34;做事,你应该看看Bootstrap UI,这是用纯角度编写的Twitter Bootstrap,不依赖于jQuery。

由于Angular控制器,指令和模态模板之间的紧密集成,我认为这将为您提供更清晰,更简单,更易于维护的方式来创建此模态。

这里是直接链接到Bootstrap UI Modal演示和示例代码的链接:
https://angular-ui.github.io/bootstrap/#/modal

如果您有兴趣沿着这条路走下去,我会添加并编辑这个答案,以便做一些与您在上面做的Bootstrap UI模式相同的事情。它将删除所有这些HTML字符串,并用漂亮的干净数据绑定替换它们。

修改 这是一个更多的&#34; Angular Way&#34;动态构建模态:

JS:

class OneNoteMultiPartRequest {
    protected $boundary;
    protected $parts = array();
    protected $responseInfo = null;
    protected $errorMessage = null;

    function __construct($presentationPartHtml) {
        $this->boundary = uniqid('', true);
        $this->appendPart("Presentation", "text/html", $presentationPartHtml);
    }

    public function appendPart($partName, $partContentType, $partContent) {
        $this->parts[] = "--{$this->boundary}\r\nContent-Disposition: form-data; name=\"$partName\"\r\nContent-Type: $partContentType\r\n\r\n$partContent\r\n\r\n";
    }

    public function send($url, $accessToken) {
        $cUrl = curl_init();
        curl_setopt($cUrl, CURLOPT_URL, $url);
        curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cUrl, CURLOPT_HEADER, false);
        curl_setopt($cUrl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($cUrl, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($cUrl, CURLOPT_VERBOSE, true);
        curl_setopt($cUrl, CURLOPT_POST, true);
        curl_setopt(
            $cUrl,
            CURLOPT_HTTPHEADER,
            array(
                "Content-Type: multipart/form-data; boundary={$this->boundary}",
                "Authorization: Bearer $accessToken"
            )
        );

        $postData = implode($this->parts) . "--{$this->boundary}--";

        curl_setopt($cUrl, CURLOPT_POSTFIELDS, $postData);        
        $response = curl_exec($cUrl);
        $this->responseInfo = curl_getinfo($cUrl);
        $this->errorMessage = curl_error($cUrl);
        curl_close($cUrl);

        return $response;
    }

    public function getResponseInfo() {
        return $this->responseInfo;
    }

    public function getErrorMessage() {
        return $this->errorMessage;
    }
}

$presentationPartHtml = <<<PRESENTATIONHTML
<!DOCTYPE html>
<html>
    <head>
    <title>Title of the captured OneNote page</title>
    <meta name="created" content="2013-06-11T12:45:00.000-8:00"/>
    </head>
    <body>
        <p>This is a simple multi-part HTML page.</p>
        <img src="name:image1"/><br/>
        <img src="name:image2"/><br/>
        <object data-attachment="Logo.png" data="name:image3" type="image/png"/>
    </body>
</html>
PRESENTATIONHTML;

$imageData = "iVBORw0KGgoAAAANSUhEUgAAAMMAAACACAYAAAC2s91oAAAAAXNSR0ICQMB9xQAAAAlwSFlzAAAXEgAAFxIBZ5/SUgAAABl0RVh0U29mdHdhcmUATWljcm9zb2Z0IE9mZmljZX/tNXEAABJzSURBVHja7Z2JV1R3ssfnj5nzJntiNAnGOMlLJiaZyTpnkkky2SbvZfIymRiRpdlBFtkUkF0UEALuiqIowQ0VV3DBJSriAij7KsiO9e63mtuCSnfTcLtvN1WeOqgHsbvv7/Nbqr6/qt+RmOZ2t6OHmq83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foewSVR+vYhWoLvzYFfbmG016HNPOA8ODNqjAcaflDjVdb6IL+87ToZxS2hSwjtK/TmblI6prY1/K9UzHrCTqKjLVwsL6Dq3q8z6Dq5rDYTBnnY2d3HfhdGEF7UsroS3BG2n1/6ZT7J8jWSMPPQyKAqsqSfwZ6klXWUn0mnQTGHRivV291FbXRrWVNVRecJL2pBRT3qI1lPxpPMW8E8H9GgCF4Qn30ZXE29gzwQnPIQKDwGCT4b5t47VGunL4Eh1dW0Y7Y7ZzT4WUzxIo6s0wPoP4Petl6t/Aq4h6cNfpVktgEBimzUaGhnmr1VBVbzy0rz9C28O3UNb3K/nQHvH6Eo7WABA09uZDuwIKABEYBIYZYYCkr6eP20NVFp+hvam/8nkk8/sMWvHxcg4fckMSZZuFyJZ6FhkX/hUYBAZXNkS2cB65Xn6NGyceyNzHravSv0rm5oWIrau1TxmUZz35AK/VSiIwCAy6MjQ17O/p5+1WzdmbdHLLcdodt5NDnmlfJhlzJMpWiw/sTy02AjLbMC1JRIFBYHAa67vTR43VDXSp9CKd2HiMipYV0jqvPEr8JI7C/hjM4V8c0tWGi2oS0dqVRGAQGJzaENWCPKHlZjNdOniRDueWsmQh49tULqGCOkIY6IYn3dn5TDLHYMyNuI1XhAoMAoPLWndrN904dY0qd5+hAxl7aWvoJsr810ouoRL8kq9p9eB+DIp7PfYzZf2wSmAQGGaGcRKxtpXqLtTyJR5k2tFFJ/nTBBbn5S3OERgEhpltOLi31LRQ/eXb/HsxgUFMzG72/5T2MiEABwKaAAAAAElFTkSuQmCC";

$oneNoteMultiPartRequest = new OneNoteMultiPartRequest($presentationPartHtml);
$oneNoteMultiPartRequest->appendPart("image1", "image/png", base64_decode($imageData));
$oneNoteMultiPartRequest->appendPart("image2", "image/png", base64_decode($imageData));
$oneNoteMultiPartRequest->appendPart("image3", "image/png", base64_decode($imageData));
$response = $oneNoteMultiPartRequest->send("https://www.onenote.com/api/v1.0/pages", "<YOUR ACCESS TOKEN>");

var_dump($response);
echo '<br/><br/>';

$info = $oneNoteMultiPartRequest->getResponseInfo();
var_dump($info);
echo '<br/><br/>';

$errorMessage = $oneNoteMultiPartRequest->getErrorMessage();
var_dump($errorMessage);

HTML:

angular.module('dynamicModalExample', ['ngRoute', 'ui.bootstrap'])

 .controller('MainController', function($scope, $location, $modal) {

     $scope.data = {
         name: 'sampleName',
         waivers: {
             id: 'sampleId',
             link: 'sampleLink',
             title: 'sampletitle'
         }
     }

     $scope.urlExtract = $location.path()
     $scope.foundExtract = $scope.urlExtract.split("#/")[1];

     if(foundExtract == "zsexdr"){
        $scope.showModalBtn = true;
     }else{
        $scope.showModalBtn = false;
     }

     // Might want to use a ternary operator instead of if/else like this:
     // $scope.showModalBtn = foundExtract == "zsexdr" ? true : false;

     $scope.open = function () {
          $modal.open({
          animation: true,
          templateUrl: 'myModalContent.html',
          controller: 'ModalInstanceCtrl',
          resolve: {
              items: function () {
              return $scope.data;
              }
          }
        }
     });
 })

 .controller('ModalInstanceCtrl', function ($scope, $modalInstance, data) {

     $scope.waiverModalLinks = data.waivers;
     $scope.name = data.name;

     $scope.ok = function () {
       $modalInstance.close();
     };
 });