2015-11-02 39 views
4

我已經創建了一個像這樣的表格。如何使用ajax加載DIV中的AngularJs頁面

<table> 
<tr> 
<th>Name<th> 
<th>Dept<th> 
<th>Num<th> 
</tr> 
<tr onclick="detail('xyz','1')"> 
    <td>abc</td> 
    <td>xyz</td> 
    <td>1</td> 
</tr> 
</table> 
<div id='content'></div> 

雖然AJAX的onclick頁面將被稱爲

function detail(dept,no){ 
$.ajax({ 
    url:"det.php?dept="+dept+"&no="+no, 
    success:function(data){ 
     $.getScript("../bootstrap/js/user_det.js"); 
     document.getElementById('content').innerHTML=data; 
    } 
}); 

在det.php頁的user_det.js將根據傳遞的參數動態地創建。

<script src='bootstrap/js/user_det.js'> </script> 
<div ng-app="myApp" ng-controller="MyController"> 
    <div ng-repeat = "val in sample_det"> 
    <div>{{val.dept}}</div> 
    <div>{{val.id}}</div> 
    <div>{{val.date}}</div> 
    </div> 
</div> 

如果我單獨運行det.php頁angularjs頁面運行完美,但阿賈克斯成功功能的頁面加載不正確。所以我需要一些解決方案來加載ajax中的angularjs頁面。

+0

如果您想自由地構建視圖並首先使用ajax加載JavaScript,然後希望使用angularjs進行初始化,您可以避免使用ng-app指令並手動初始化angularjs,如[這裏]所述(https://docs.angularjs .ORG /導向/引導)。讓我們知道這是你想要的還是如果這是一個選項。 – amitthk

回答

4

你可以添加一個指令來加載你的html和js文件。

HTML:

<div id='content' load-content></div> 

JS:

app.directive('loadContent', function($compile) { 
    return { 
    link: function(scope, elem, attrs) { 

     $.ajax({ 
     url: "det.php?dept=" + dept + "&no=" + no, 
     success: function(data) { 

      //append this js into this page header 
      $.getScript("../bootstrap/js/user_det.js"); 

      //create an angular element. (this is still our "view") 
      var el = angular.element(data); 

      //compile the view into a function. 
      compiled = $compile(el); 

      //append our view to the element of the directive. 
      elem.append(el); 

      //bind our view to the scope! 
      //(try commenting out this line to see what happens!) 
      compiled(scope); 
     } 
     }); 
    } 
    }; 
}); 

插入點擊方法

$.ajax({ 
    url: "det.php?dept=" + dept + "&no=" + no, 
    success: function (data) { 

     //append this js into this page header 
     $.getScript("../bootstrap/js/user_det.js"); 

     //create an angular element. (this is still our "view") 
     var el = angular.element(data); 

     //compile the view into a function. 
     compiled = $compile(el); 

     //append our view to the element of the directive. 
     var elem = angular.element("#content"); 
     elem.append(el); 

     //bind our view to the scope! 
     //(try commenting out this line to see what happens!) 
     compiled(scope); 
    } 
}); 

HTML <div id='content'></div>

+0

必須在onclick中使用這個腳本。 – Pravin

+0

我已經使用了負載。因爲在調用頁面時會自動調用指令 – Shohel

+0

這裏我使用的是onclick函數,所以我必須將動態值傳遞給det.php。是否還有其他解決方案? – Pravin

相關問題