2015-01-02 43 views
0

我在頁面上同時使用了AngularJs和Pjax。AngularJs在pjax後停止工作

當我的頁面的一部分重新加載Pjax後,Angularjs停止在那裏工作。

簡單的{{2 + 2}}除了部件之外都可以工作,用pjax重新加載。

我發現,問題在於使用$ compile。

我已經嘗試了不同的代碼片段,但他們都沒有爲我工作。

無論這個

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

也不是這

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

代碼段爲我工作。有人知道一個可行的解決方案嗎?

UPD: 在PSL將$(「body」)更改爲$(document.body)的建議後,代碼開始工作。但是現在一切都從頭開始初始化,並且在每個pjax調用之後我都會丟失我的數據(在我的示例中爲選定的圖像)。我怎樣才能避免這種情況?

這是我的js文件:

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

控制檯中的任何錯誤? – tymeJV

+0

__ ^^ __你的視圖中沒有ng-app? – PSL

+0

@tymeJV是的,我有一些錯誤,但它不是很豐富,至於我。 錯誤:[jqLit​​e:nosel] _http://errors.angularjs.org/1.2.27/jqLit​​e/nosel然後指針的地方中的代碼 全文: 錯誤:[jqLit​​e:nosel] HTTP ://errors.angularjs.org/1.2.27/jqLit​​e/nosel \t ... gify(arguments [c]):arguments [c]); return Error(a)}}函數Sa(b){如果(空== b || JA(... \t angular.min.js(第6行,列449) –

回答