angular-route-segment icon indicating copy to clipboard operation
angular-route-segment copied to clipboard

app-view-segment should clean itself up when a route has a normal controller instead of a segment

Open forivall opened this issue 11 years ago • 2 comments

I would like to be able to mix classical angular views with app-view-segment views; however, when I transition from a segment view to a normal view, the segment view remains.

Setup is as follows:

$routeSegmentProvider
    .when('/segment', 's1.home')
    .segment('s1', {
        template: 's1:<div app-view-segment="1"></div>'
    })
    .within().segment('home', {template: 'home'});
$routeProvider
    .when('/', { template: 'no segment'})

index.html contains:

<div app-view-segment="0"></div>
<div ng-view=""></div>

Plunkr - click "to segment" and then "to root"; the segment view remains.

ng-view cleans up after itself if there is no template (and even removes the element via transclusion); app-view-segment should do the same thing.

This is my current workaround:

app.run(function($rootScope, $routeSegment) {
    $rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
        var segmentsOn = false, segmentsOff = false, inPrev = false, inCurrent = false;
        if (previous != null) {
            inPrev = 'segment' in previous;
            inCurrent = 'segment' in current;
        }
        segmentsOff = inPrev && !inCurrent;
        segmentsOn = inCurrent && !inPrev;
        if (segmentsOff || segmentsOn) {
            $rootScope.$broadcast('routeSegmentChange', {
                index: 0, segment: segmentsOff ? null : $routeSegment.chain[0]
            });
        }
    });
});

forivall avatar Oct 16 '13 18:10 forivall

I am having the same problem.

zacharynevin avatar Jul 20 '14 05:07 zacharynevin

@zacharynevin Does the workaround that I posted work? Do you think that it could help you write a pull request? (I am no longer working on angular projects, so I don't see myself spending the time to do so myself.)

forivall avatar Jul 21 '14 22:07 forivall