Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

history.pushState not working in controller

I have this two buttons in my html :

<a href="" class="sbtn sbtn-default" ng-click="GoToNext()"><i class="fa fa-angle-right"></i> Next </a>
<a href="" class="sbtn sbtn-default" ng-click="GoToPrev()"> Prev <i class="fa fa-angle-left"></i></a>

then in my controller :

$scope.GoToNext = function () {
    var nextPage = getNextPage("next");

    history.pushState(null, "Site Title", nextPage);
    getQuestion();
}

$scope.GoToPrev = function () {
    var prevPage = getNextPage("prev");

    history.pushState(null, "Site Title", prevPage);
    getQuestion();
}

and the getNextPage function is :

function getNextPage(type) {

    var querystr = window.location.pathname.split('/')[4];
    var currentQuestion = querystr.split("-")[1];
    var currentBook = querystr.split("-")[0];

    switch (type) {
        case "next":
            return ((currentBook + "-") + (+currentQuestion + +1));

        case "prev":
            if (currentQuestion == 1)
                return (currentBook + "-1");

            return ((currentBook + "-") + (+currentQuestion - +1));

    }

};

my Url is look like this :

http://localhost:14799/app/Dashboard/Question/1-330

getQuestion function just get the data's from the server , I already tested it out and it return the data with no problem , actually it return the data at page load with no problem .

what I'm trying to do is changing the URL and get the next or previous question by clicking on those buttons . but what is happening is it calls the getQuestion function but the URL is not changing . if i look very closely to the URL and pay attention it get change and get back to previous page very very fast , just a flash !

I have nothing in my code to return to previous page .

what I try is add console.log in getNextPage function to see if it call twice , but it doesn't , it just call once for every click .

if I try to pushState in console , it work !

I also add console log at the end of my getQuestion function to see if its get stuck in the middle of the function , but no ! console log is happening but it return to the previous page .

I just desperately testing things , but no luck .

like image 507
M.R.Safari Avatar asked Mar 23 '15 18:03

M.R.Safari


1 Answers

year a go i was struggling with this problem and now i understand it. its not how angular works. i should of used ngRoute module to provide navigations. its not about the pushstate not working or something else, its about how angular works.

i don't need $scope.GoToNext or $scope.GoToPrev for navigating because angular would do it for me automatically. just change the route and its done. everything works fine.

like image 88
M.R.Safari Avatar answered Sep 22 '22 15:09

M.R.Safari