Loading Partial Page With Angular and Compile The Controller

前端 未结 2 546
再見小時候
再見小時候 2020-12-29 15:41

In large scale application, our web application might be organize into separate partial page in order to increase the modularity of our application. In some case compiling a

相关标签:
2条回答
  • 2020-12-29 16:12

    There's nothing wrong on AngularJS on this setup, others JS guru out there might already know the solution and very busy to share with us while inventing another cool web development tools or framework. That's OK keep doing that. This might not be a cool or ultimatum solution, please share any improvement or tips with us!

    To overcome this problem we need s strategy to setup, let me start with an example code so our brain will digest while the information flowing through. Below code is the placeholder where i create the the modal dialog using JQuery and the Ajax content will be insert.

    <div ng-app="asng" id="dialog" title="" style="display:none">
         <div id="dialog-content"></div>
    </div>
    

    As a basic knowledge, we have to understand on how DOM parser are working. We might think that DOMP (DOM Parser) is a multi-threaded and that's the reason we can load multiple external resource in parallel. Actually DOMP are single threaded while parsing the DOM elements index from top to bottom. Below is the example on the partial page that I'm gonna load into #dialog-content DIV element.

    <script language="JavaScript" type="text/javascript">
        function Transaction ($scope,$http){
            $scope.items = [{"country":"VN","quantity":"100"}];
            $scope.country_name = $scope.items;
        }
    </script>
    
    <style>
    </style>
    
    <div id="transaction-panel" class="user" data-ng-controller="Transaction">
            <form id="{{ form_name }}" action="">
            Country : [[ items.country ]] </br>
            Total : [[ items.quantity ]]
        </form>
    </div>
    

    Actually these partial still giving an error, although we have put the script block just before the element with ng-controller directive. Actually that's not really the case, the part that we need to tackle is how AngularJS compile service are compiling the partial DOM. Let go back on my question part above and inspect where the line that we do the compile thing.

    html = $compile(data)(scope);
    $('#dialog-content').html(html);
    

    First line above will compile DOM in data variable, and insert into the root DOM unfortunately first line will shout an error : Controller Transaction not found.

    This happen because, the Script Block in your partial page is not yet evaluate by DOMP parser because is not inserted into the root DOMP. Now you see the light OK, so we have to change the compiling strategy a bit, by inserting the new DOM and then we will parse back the inserted DOM look example below:-

    html = $('#dialog-content').html(data);
    $compile(html)(scope);
    

    Slim and simple solution, it took me few head banging morning to solve this problem just because ignoring the simple concept on DOM parsing.

    0 讨论(0)
  • 2020-12-29 16:15

    If I understand what you are trying to do, here's a simple example.

    I wanted to post via AJAX to a Django form and then replace the form content in the page with the returned markup. The returned markup includes an ng-controller, which I need to execute when it loads:

    .controller('MyForm', function($element, $compile, $scope){
        var scope = $scope;
        var $theForm = $element;
        var $formBlock = $element.find('.the_form');  // is replaced by the form response
        $element.find('.submit_the_form').click(function(){
            // submit the form and replace contents of $formBlock
            $.post($theForm.attr('action'), $theForm.serialize(), function(response){
                var newstuff = $formBlock.html(response);
                $compile(newstuff)(scope); // loads the angular stuff in the new markup
            });
        });
    })
    

    I think the line you're interested in is $compile(newstuff)(scope);

    EDIT: Crikey, tried this with some other markup this morning and did not work, for no reason I could figure out. Turned out that if I did not have a field with ng-model assigned, in the new markup, then the $compile does not execute. Added:

    <input type="hidden" name="dummy" value="0" ng-model="dummy"/>
    

    ...and now it compiles.

    0 讨论(0)
提交回复
热议问题