如何使用$ compile?

时间:2015-08-04 15:46:16

标签: angularjs angularjs-controller

我尝试从角度控制器注入模板html。我搜索了如何使用$ compile。所以,我编程了但是它不起作用:

  CustomersCtrl.$inject = ['$scope', '$http', '$location', '$modal', '$templateCache', '$window', '$compile'];

function CustomersCtrl($scope, $http, $location, $modal, $window, $compile) {

    var htmlContent = $('#pagination');
    htmlContent.load('Partials/Customers/pagination.html');
    $compile(htmlContent.contents())($scope);}

错误是:

TypeError:$ compile不是函数

欢迎所有帮助。

1 个答案:

答案 0 :(得分:0)

对于初学者,DOM代码不属于控制器。

接下来,当angular已经提供了许多方法时,使用jQuery加载这个内容是没有意义的。

一种简单的方法是使用ng-include

<div id="pagination" ng-include src="'Partials/Customers/pagination.html'">

使用这样的指令意味着您不需要自己编译,它将自动为您完成。

我留下了ID,但角度视图中很少需要ID。

我强烈建议你在学习如何使用角度时删除所有jQuery的使用。请参阅:"Thinking in AngularJS" if I have a jQuery background?

相关问题