2015-11-02 38 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

相关问题