ng-View无法在单页应用程序中运行

时间:2016-08-14 06:09:41

标签: javascript html angularjs ng-view

我正在努力让Angular SPA第一次运作。并遇到一些问题。我似乎无法让ng-view正确渲染或路由。似乎无法解析ng-view

文件结构

enter image description here

mainController.js

var propManApp = angular.module('propManApp', ['ngRoute']);

propManApp.config(function ($routeProvider){
$routeProvider
.when('/', {
    templateUrl : '../public/main.html',
    controller  : 'mainController'
})

.when('/about', {
    templateUrl : '../public/about.html',
    controller  : 'aboutController'
})

.when('#/contactUs', {
    templateUrl : '../public/contactUs.html',
    controller  : 'contactUsController'
})

.otherwise({redirectTo : 'index.html'});
});



propManApp.controller('mainController',['$scope', '$log',
function($scope, $log){
$scope.message = "Message Here!!!!!!";
}]);

propManApp.controller('aboutController',['$scope', '$log',
function($scope, $log){
$scope.aboutMessage = "About Message Here!!!!!!";
console.$log("Test");
}]);


propManApp.controller('contactUsController',['$scope', '$log',
function($scope, $log){
$scope.contactMessage = "Contact Message Here!!!!!!";
}]);

的index.html

<!DOCTYPE html>
<html lang="en">
<head ng-app="propManApp">
    <title>Rental Properties</title>
 <!-- angular/  -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js">    </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-route.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">     </script>
<!-- bootstrap -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

</head>
<body>

<nav class="navbar navbar-inverse navbar-static-top">
<div class="container-fluid">
<!-- Logo -->
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-        toggle="collapse" data-target="#mainNavBar">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
            <a href="#" class="navbar-brand">Rental PROPERTIES</a>
    </div>

    <!-- Menu Items -->
    <div class="collapse navbar-collapse" id="mainNavBar">
        <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#/about">About</a></li>
            <li><a href="#/contact">Contact</a></li>
        </ul>
    </div>

</div>
</nav>

<div class="container">
    <div ng-view></div>
</div>
</body>

main.html中

<h1>{{message}}</h1> Main

about.html

<h1>{{aboutMessage}}</h1> About

浏览器检查器:

enter image description here

1 个答案:

答案 0 :(得分:1)

您已将ng-app标记设置为head而非html标记。这意味着您的应用仅围绕头标签,并且不包括使用ng-view的body标签。

所以在index.html中

<html lang="en" ng-app="propManApp">
<head >
    <title>Rental Properties</title>
 <!-- angular/  -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js">    </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-route.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">     </script>
<!-- bootstrap -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

</head>

像这样改变它。

此外,您还没有包含mainController.js。在jquery文件之后将它包含在index.html中。