angularjs $routeProvider - route not found

二次信任 提交于 2020-01-01 22:00:39

问题


I am using angularjs with requirejs. I am running into a problem where my routes are not being routed properly. It's constantly routing to /. If I change /post to /:post it hits my post route.

// module/post.js
define([], function () {
    'use strict';

    var app = angular.module('myApp.post', [])
    .config(['$routeProvider',
        function($routeProvider) {
            $routeProvider.when('/post', {
                    controller: 'PostController',
                    template: "<div>{{page}}</div>"
                }
            );
        }
    ]);

    return app;
});


// main.js
require.config({
    baseUrl: '/static/js',
    paths: {
        angular: 'vendor/angular/angular',
        jquery: 'vendor/jquery/jquery',
        _: 'vendor/underscore/underscore'
    },
    shim: {
        angular: {
            exports: angular
        }
    }
});

require([
    'module/post'
], function() {
    'use strict';

    var app = angular.module('myApp', [
        'myApp.post'
    ])
    .config(['$routeProvider',
        function($routeProvider) {
            $routeProvider.otherwise({
                redirectTo: '/'
            });
        }
    ]);

    $(function(){
        angular.bootstrap(document, ['myApp']);
        $('html').addClass('ng-app: myApp');
    });
});

回答1:


Dumb mistake on my part. I'll leave this question opened in case someone runs into this problem.

I did not have html5 mode on

var app = angular.module('myApp.post', [])
    .config(['$routeProvider', '$locationProvider',
        function($routeProvider, $locationProvider) {
            $routeProvider.when('/post', {
                    controller: 'PostController',
                    template: "<div>{{page}}</div>"
                }
            );
            $locationProvider.html5Mode(true);
        }
    ]);

otherwise my urls would map with hashbang localhost/#post instead of /post



来源:https://stackoverflow.com/questions/18584703/angularjs-routeprovider-route-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!