Saturday, October 14, 2017

Angularjs-probelm dont work $compile after get response of ajax

i created directive for making print page.this directive have template page that included button and print template(this have ngRepeat on Result object).print button was clicked fire “click” funciton in Controller of directive then ajax request send to server for get array of object that fill print tamplate.

Mainpage.html

<div ng-class="{'btn':true,'btn-warning':true,'disabled':disableBtn}" data-toggle="modal"
                     ng-click="getdetail=true;" data-target="#detailModal">order detail</div>
                     <print-order disable-btn="disableBtn" order="selectedItem"></print-order>

print.template.html

    <a ng-click="ctrl.click()" ng-class="{'btn':true,'btn-info':true,'disabled':ctrl.disableBtn}" >
print
    <i class="fa fa-print"></i>
</a>   
<div  id="printSection">
    <table style="width: 100%;font:13px tahoma;" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <th style="border-bottom: 3px solid black;">
                    <p>number: </p>
                    
                </th>
                <th>
                    <img src="/images/receipt-logo.png" alt="">
                </th>
                <th style="border-bottom: 3px solid black;">
                    <p>code :</p>
                    <p> 
                        <br> 
                    </p> 
                </th>
            </tr>
        </thead>
        <tbody>
            <tr style="border-bottom: 3px solid black;">
                <td colspan="3" style="padding: 10px;line-height: 20px;">
                    cutomer : 
                <br /> address: 
                <br/>mobile : 
                <br/> phone :  </td>
            </tr>        
        </tbody>        
    </table>
    <h1></h1>
    <table dir="rtl" width="100%" border="0" cellspacing="0" cellpadding="0" align="center" style="margin-top:20px;border-top:2px solid #000000;;border-bottom:2px solid #000000;border-color: #000000">
        <tbody>
            <tr>
                <td width="40%" style="padding-right:10px;font-size: 10px" align="right">name</td>
                <td width="20%" style="font-size: 10px" align="center">number</td>
                <td width="20%" style="font-size: 10px" align="center">price</td>
                <td width="25%" style="font-size: 10px" align="right">price</td>
            </tr>
            <tr ng-repeat="item in ctrl.Components track by $index">
                <td style="padding-right:10px;font-size: 9px">
                    <br>
                </td>
                <td style="font-size: 12px" align="center"></td>
                <td style="font-size: 10px" align="center"></td>
                <td style="font-size: 10px;padding: 5px"></td>
            </tr>
        </tbody>
    </table>
</div>

printdirective.js

myApp.directive("printOrder",["$window","orderService","$timeout","$compile",function($windows,orderService,$timeout,$compile){
return{
    restrict:"AE",
    bindToController:{
        disableBtn:"=",
        order:"="
     },
    templateUrl:"/Widgets/printOrder.template.html",        
    transclude: true,
    scope:true,
    controllerAs:"ctrl",
    controller:function(){                             
        this.click=function(){               

            var popupWinindow =$windows.open("", '_blank', 'width=300,height=500');
             orderService.getOrderDetail(this.order.id)
             .then(function(result){                    
                this.Components=result;                    
                popupWinindow.document.open();                    
                var el=angular.element("#printSection")
                $compile(el)(this);
                $timeout(function(){                                        
                   // console.log(el.html());                                        
                    popupWinindow.document.write(
                           `<html>
                                <head></head>
                                <body style="direction: rtl;">`+el.html()+` </body>
                            </html>`);                            
                    popupWinindow.document.close();
                },500)                                        
            });
        }
    },        
}

}])

when i clicked on print button .id of order send to directive then detail of order request of server with ajax that this should fill “#printSection” of template by $compile but this dont binding and Components property is empty.

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/10/14/angularjs-probelm-dont-work-compile-after-get-response-of-ajax/
via @lzomedia #developer #freelance #web #lzomedia.com

No comments:

Post a Comment