Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

ngView inside ngSwitch causes $routeProvider to load controller twice. #5754

Closed
worldspawn opened this issue Jan 13, 2014 · 2 comments
Closed

Comments

@worldspawn
Copy link

Hi i have the following markup:

        <div class="main-content" ng-switch on="errorState" ng-cloak>
            <div class="container" ng-switch-when="403">
                <div ng-include="'/public/views/error/index.html'"></div>
            </div>
            <div class="container" ng-switch-default>
                <div ng-view></div>
            </div>

            <footer class="site-footer">
                <div class="container">
                    <ul>
                        <li ng-repeat="node in menuNodes | filter: {showInFooter:true}"><a href="{{node.path}}">{{node.menuTitle || node.title }}</a></li>
                    </ul>
                </div>
            </footer>
        </div>

And a route (but this applies to all my routes)

$routeProvider.when('/roundtables', {
        templateUrl: '/public/views/roundtable/index.html',
        controller: 'roundtableController'
    });

When a browse directly to (meaning i type into the address bar) /roundtables' theroundtableController` is initialised twice.

If i enter the url /roundtables/ (has trailing slash) the controller is initialised only once.

If i take my ng-view and put it outside of the ng-switch i do not get this behaviour.

All angular files loaded with:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular-route.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular-resource.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular-sanitize.min.js"></script>
@worldspawn
Copy link
Author

I seem to have worked around this by representing my default state with an ng-if outside of the switch.

<div class="main-content" ng-switch on="errorState" ng-cloak>
            <div class="container" ng-switch-when="403">
                <div ng-include="'/public/views/error/index.html'"></div>
            </div>
            <div ng-if="errorState === null" class="container">
                <div ng-view></div>
            </div>

            <footer class="site-footer">
                <div class="container">
                    <ul>
                        <li ng-repeat="node in menuNodes | filter: {showInFooter:true}"><a href="{{node.path}}">{{node.menuTitle || node.title }}</a></li>
                    </ul>
                </div>
            </footer>
        </div>

@worldspawn
Copy link
Author

I still have a similar problem in ie9 #6104

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant