I'm having an issue developping my website in AngularJS during the implementation of the back and forward buttons.
// app.js
var app = angular.module('myApp', [])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/Page1', {
templateUrl: 'partial/view1.html', controller: Ctrl1})
.when('/Page2', {
templateUrl: 'partial/view2.html', controller: Ctrl2})
.otherwise({
redirectTo: '/Page1'});
}]);
When I navigate from page 1 to page 2 I can't go back even using the following directive :
// app.js
app.directive('backButton', function(){
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', goBack);
function goBack() {
history.back();
scope.$apply();
}
}
}
});
-
<!-- view1.html / view2.html -->
<i class="fa fa-chevron-left" back-button></i>
Yet it works when I back on the same view. Is there a solution ?
Thanks
Edit: $window doesn't help either since I've tried changing my directive as such :
// app.js
app.directive('backButton', ['$window', function($window) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', goBack);
function goBack() {
$window.history.back();
scope.$apply();
}
}
}
});
and got the same behavior.