Angular JS ngView和ngRoute不工作

Angular JS ngView and ngRoute not working

本文关键字:工作 ngRoute JS ngView Angular      更新时间:2023-09-26

我是angularJS的新手。

我正在尝试用ng-routeng-view制作一个小角度的应用程序。当用户单击登录按钮时,URL将从localhost:8000更改为localhost:8000/login

现在,如果用户重新加载页面,/login的视图将加载到页面加载。

这是我有ng-appng-controller的HTML文件。

<html ng-app="loginSignUpApp">
   <head>...</head>
   <body ng-controller="mainController">
       <div ng-view></div>
   </body>
</html>

现在,如果用户从localhost:8000开始并点击登录按钮,那么一切都很好。但在点击登录按钮后,如果用户重新加载页面,则不会触发loginController。视图未进行渲染。

var app = angular.module('loginSignUpApp', ['ngMaterial', 'ngRoute', 'ngAnimate']);
app.controller('mainController', function($scope, $route, $routeParams, $location, $window, serviceFunctions) { 
    $scope.gotoPath = function (url) {
        $location.path( url );
        if(!$scope.$$phase) $scope.$apply()
    }
})
.controller('loginController', function($scope, $route, $routeParams, $location, $window, serviceFunctions, $http) {
    // some work...
})
.config(function($routeProvider, $locationProvider) {
    $routeProvider
    .when('/login', {
        title : 'Login',
        templateUrl: 'signIn.html',
        controller: 'loginController'
    })
    .otherwise({
        title : 'Landing Page',
        redirectTo:'/', 
        controller: 'mainController'
    })
    $locationProvider.html5Mode(true);
})
.run(['$rootScope', '$route', function($rootScope, $route) {
    $rootScope.$on('$routeChangeSuccess', function() {
        angular.element('title').html($route.current.title);
    });
}]);

我使用Django作为视图渲染的后端和angular。

任何形式的帮助都将不胜感激。提前感谢。

您应该配置您的服务器,因为当您使用html5mode时,您的服务器无法划分后部和前部。下面是Apache的一个例子https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/