Loading Partial Page With Angular and Compile The Controller

前端 未结 2 564
再見小時候
再見小時候 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.

    
    

    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.

    
    
    
    
    
    Country : [[ items.country ]]
    Total : [[ items.quantity ]]

    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.

提交回复
热议问题