0

I am using both AngularJs and Pjax on the page.

After a part of my page is reloaded with Pjax, Angularjs stops working there.

Simple {{ 2+2 }} works everywhere except the part, reloaded with pjax.

I have found, that the problem is in using $compile.

I have tried different code snippets, but none of them worked for me.

Neither this one

  angular.element(document).ready(function() {
     angular.bootstrap(document);
   });

nor this

// outside of angular...
  var ngRefresh = function() {
  var scope = angular.element("body").scope();
  var compile = angular.element("body").injector().get('$compile');

  compile($("body").contents())(scope); 
  scope.$apply();
}

// PJAX
$('#page').on('pjax:success', function(e){
  ngRefresh();
});

code snippets worked for me. Does anybody know a working solution?

UPD: After PSL's advice to change $("body") to $(document.body) the code started to work. But now everything is initialized from scratch and I loose my data (selected image in my example) after every pjax call. How can I avoid this?

This is my js file:

app = angular.module("BlogImageApp", []);

app.controller("BlogImageController", function($http, $scope){

$scope.selectedImageId = null;
$scope.selectedImageUrl = "";

$scope.selectImage = function(image_id) {
    $scope.selectedImageId = image_id;
    var params = 'id='+image_id;
    $http.get('/blog/image/get-url-by-id?'+params).success(function(data){
        $scope.selectedImageUrl = data;
    });

}
});

// registers the BlogImageApp in the view

angular.bootstrap(document, ['BlogImageApp']);
  • 1
    Any errors in the console? – tymeJV Jan 02 '15 at 17:14
  • __^^__ And you do not have ng-app in your view right? – PSL Jan 02 '15 at 17:20
  • @tymeJV Yes, I have the some error, but it's not very informative, as for me. Error: [jqLite:nosel] _http://errors.angularjs.org/1.2.27/jqLite/nosel and then a pointer to a place in the code Full text: Error: [jqLite:nosel] http://errors.angularjs.org/1.2.27/jqLite/nosel ...gify(arguments[c]):arguments[c]);return Error(a)}}function Sa(b){if(null==b||Ja(... angular.min.js (line 6, col 449) – Vladimir Shevchenko Jan 02 '15 at 17:20
  • @PSL **And you do not have ng-app in your view right?** No, I used angular.bootstrap(document, ['BlogImageApp']); in the .js file – Vladimir Shevchenko Jan 02 '15 at 17:23
  • @VladimirShevchenko Ok just wanted to verify that.. You cannot do `angular.element("body")` you would need to do `angular.element(document.body)`. jqLite does not support tag name look up And for manual bootstrapping you would need to provide appname and other dependent modules as well. `angular.bootstrap(document, ['myApp']);` – PSL Jan 02 '15 at 17:24
  • @PSL Thanks, your advise helped a lot. Changing $('body') to document.body helped. But I still loose my current data saved in the controller, when I use Pjax. I have UPDATED my post above to show this. Is there a way to save data from the controller(current_image) between Pjax calls? – Vladimir Shevchenko Jan 02 '15 at 17:47

1 Answers1

0

It could be a race condition between loading the div and running the code contained in it. There is a bit of jquery here that forces the angularjs to load after the div pjax loads.

https://masonoise.wordpress.com/2013/05/01/pjax-and-angularjs/

David Newcomb
  • 10,639
  • 3
  • 49
  • 62