0

I try to inject a template html from an angular controller. I searched how to use $compile. So, I programmed that but it doesn't work:

  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);}

The error is :

TypeError: $compile is not a function

All help is welcome.

JonathanTheBrosh
  • 208
  • 3
  • 14

1 Answers1

0

For starters DOM code does not belong in a controller.

Next it makes no sense to load this content using jQuery when angular already provides numerous ways to do it.

One simple way is using ng-include

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

Using a directive like this means you don't need to compile yourself , it will automatically be done for you.

I left the ID in however ID's are rarely needed in angular views.

I would strongly suggest you remove all use of jQuery when learning how to work with angular. See: "Thinking in AngularJS" if I have a jQuery background?

Community
  • 1
  • 1
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • I tried ng-include, it display my partial. But if I tried to use $compile it's to refresh the pagination div. In my application, I have make a select to change the number of customers in my table and it's work but the pagination isn't refresh. So, is it possible to bind the ng-include with ng-model or other tag to refresh the div on action or on change value of my variable. – JonathanTheBrosh Aug 05 '15 at 08:10
  • Finally, I found the problems. it's because i reload my scope at the wrong time. Thanks for all. And I keep your solution charlietfl with ng-include. You alright, it's more clean. – JonathanTheBrosh Aug 05 '15 at 09:39
  • You will have lots of similar problems if you continue using jQuery for things in angular that angular already has methods for. Feel free to accept this answer if it helped – charlietfl Aug 05 '15 at 11:10