angularjs html5模式相当url无法正常工作

时间:2016-01-27 08:11:04

标签: angularjs angularjs-directive angularjs-scope angular-ui-router

我正在做一个由网络风暴生成的angularjs应用程序。 我试图使用html5模型相当url(没有#在url上)。它不工作。带#的网址工作正常。 这是我的训练 的 app.js

var app = angular.module('MyApp', [
    'ngRoute',
    'ngCookies',
    'MyApp.login',
    'MyApp.services',
    'MyApp.AuthenticationService',
    'headroom',
    'angular-loading-bar',
    'angularSoundManager',
    'updateMeta'
]).
    config(['$routeProvider','$locationProvider', function ($routeProvider,$locationProvider) {
        $routeProvider.when('/login', {templateUrl: 'user/login.html', controller: 'loginCtrl'});
        $routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
        $routeProvider.otherwise({redirectTo: '/login'});
        // configure html5 to get links working on node-webkit
        if(window.history && window.history.pushState){
            //$locationProvider.html5Mode(true); will cause an error $location in HTML5 mode requires a  tag to be present! Unless you set baseUrl tag after head tag like so: <head> <base href="/">

            // to know more about setting base URL visit: https://docs.angularjs.org/error/$location/nobase

            // if you don't wish to set base URL then use this
            $locationProvider.html5Mode({
                enabled: true,
                requireBase: false
            });
        }
    }])

index.html主管部分

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>MyApp</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="content/images/favicon.png" type="image/x-icon"/>
    <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/normalize.css">
    <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/main.css">
    <link rel="stylesheet" href="app.css">
    <link rel="stylesheet" href="bower_components/angular-loading-bar/build/loading-bar.min.css">
    <script src="bower_components/html5-boilerplate/dist/js/vendor/modernizr-2.8.3.min.js"></script>
    <base href="/">
</head>

login.js

'use strict';

    angular.module('MyApp.login', ['ngRoute'])
        .controller('loginCtrl', ['$scope', '$location', '$http', 'MyAppSettings', 'apiService','AuthenticationService', function ($scope, $location, $http, MyAppSettings, apiService,AuthenticationService) {
            (function initController() {
                alert('d');
                // reset login status
                //AuthenticationService.ClearCredentials();
            })();
            function _login(username, password) {
                AuthenticationService.Login(username, password, function (response) {
                    if (response.success) {


                    } else {
                        alert('error')
                    }
                });

            }
            $scope.login=_login;
        }]);

有人可以帮忙吗?先谢谢。

0 个答案:

没有答案
相关问题