ionic-team / ionic-v1

The repo for Ionic 1.x. For the latest version of Ionic, please see https://github.com/ionic-team/ionic
Other
193 stars 187 forks source link

Location replace not working on Android #241

Open jgw96 opened 7 years ago

jgw96 commented 7 years ago

From @lchanouha on April 11, 2017 19:1

Ionic version: (check one with "x") 1.3.3

I'm submitting a ... (check one with "x") [ x ] bug report

Current behavior:

location replace has no effect on android platform, but is OK on serve on browser. When I push back button (system or top ionic back button), i go the te wrong state

A -> B -> C [back] comes to A on ionic serve (expected behavior) A -> B -> C [back] comes to B on android

The replace is asked for transition from B to C

Example of link:

I also tried with custom joined directive joined below (to use $state.go), but i have the same results.

Steps to reproduce:

directive('locReplace',$StateRefDirective);

function $StateRefDirective($state, $interpolate, $timeout, $rootScope) {

    function parseStateRef(ref) {
        var parsed = ref.match(/^([^(]+?)\s*(\((.*)\))?$/);
        if (!parsed || parsed.length !== 4) throw new Error("Invalid state ref '" + ref + "'");
        return { state: parsed[1], paramExpr: parsed[3] || null };
    }

    function hasToBeInterpolated(attributeValue) {
        return attributeValue.indexOf("{{") != -1
    }

    function isForm(element) {
        return element[0].nodeName === "FORM";
    }

    function buildNewHref(attributeValue, params) {
        return"/#" + $state.href(attributeValue.state, params, { lossy: true });
    }

    function setUrl(element, attr, newHref) {
        element[0][attr] = newHref;
    }
    return {
        restrict: 'A',
        priority: 1000,
        scope: '=',
        link: function (scope, element, attrs) {
            var attributeValue = attrs.uiSref;

            if (hasToBeInterpolated(attributeValue)) {
             attributeValue = $interpolate(attributeValue)(scope);
             }

             attributeValue = parseStateRef(attributeValue);

             var params = null
             var url = null;
             var attr = isForm(element) ? "action" : "href"; //, nav = true;

             var update = function (newParams) {
             if (newParams) {
             params = newParams;
             }
             var newHref = buildNewHref(attributeValue, params);
             if (!newHref) {
             nav = false;
             return false;
             }
             setUrl(element, attr, newHref);
             };

             if (attributeValue.paramExpr) {
             scope.$watch(attributeValue.paramExpr, function (newParams, oldVal) {
             if (newParams !== oldVal) {
             update(newParams);
             }
             }, true);
             params = scope.$eval(attributeValue.paramExpr);
             }
             update();
            element.bind('click',function(e){
                $ionicHistory.backView($ionicHistory.backView())
                $state.go(attributeValue.state, params, {location: 'replace'} );
                e.stopPropagation();
                e.preventDefault();
                return false;
            });
        }
    };

Thanks for the help

Copied from original issue: driftyco/ionic#11176