0

我正在 AngularJS 中构建一个学习应用程序,我面临一个问题,即每当我从任何位置刷新页面时,它都会重定向到主页,例如:我打开localhost/angular/inner-page/page-id并刷新页面,然后它首先重定向到localhost/angular/index.html哪个是模板文件然后它重定向到localhost/angular.

这是代码:

// application
var myApp = angular.module("firstApp", ["ngRoute"])
// routes
.config(function($routeProvider, $locationProvider) {
    var baseUrl = "../includes/016-routing/";

    $locationProvider.html5Mode(true);
    $routeProvider.when("/home", {
        "templateUrl": baseUrl + "home.html",
        "controller": "homeController"
    }).when("/courses", {
        "templateUrl": baseUrl + "courses.html",
        "controller": "coursesController"
    }).when("/students", {
        "templateUrl": baseUrl + "students.html",
        "controller": "studentsController"
    }).when("/student/:id", {
        "templateUrl": baseUrl + "student.html",
        "controller": "studentController"
    }).otherwise({
        "redirectTo": "/home"
    });
})
// controllers
.controller("homeController", function($scope) {
    $scope.heading = "Home Page";
}).controller("coursesController", function($scope) {
    $scope.heading = "Courses";
    $scope.courses = [
        "PHP",
        "CSS",
        "SCSS",
        "MySQL",
        "JavaScript",
        "jQuery",
        "AngularJs"
    ];
}).controller("studentsController", function($scope) {
    $scope.heading = "Students";
    $scope.students = [{
        "id": 1,
        "name": "Parminder Singh"
    }, {
        "id": 2,
        "name": "Kanwaljeet Kaur"
    }];
}).controller("studentController", function($scope, $routeParams) {
    // variables
    var id = ($routeParams.id > 0 || $routeParams.id) ? ($routeParams.id - 1) : 0;
    var $students = [{
        "name": "Parminder Singh",
        "age": 34,
        "profession": "PHP Developer",
        "designation": "Sr. PHP Developer"
    }, {
        "name": "Kanwaljeet Kaur",
        "age": 28,
        "profession": "House Wife",
        "designation": "Home Ministry"
    }];

    $scope.heading = $students[id].name;
    $scope.student = $students[id];
});
4

0 回答 0