简体   繁体   中英

Partial Layout in Angular JS with new latest routes

 /// <reference path="Scripts/angular.js" /> var myApp = angular.module("myModule", ["ngRoute"]) .config(function ($routeProvider) { $routeProvider .when("/home", { templateUrl: "homeHTML.html", controller: "homeController" }) .when("/courses", { templateUrl: "coursesHTML.html", controller: "coursesController" }) .when("/student", { templateUrl: "studentHTML.html", controller: "studentController" }) }) .controller("homeController", function ($scope) { $scope.message = "Home"; }) .controller("coursesController", function ($scope) { $scope.message = "Courses"; }) .controller("studentController", function ($scope) { $scope.message = "Student"; }); 
 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ng-app="myModule"> <head> <script src="Scripts/angular.min.js"></script> <script src="Scripts/angular-route.min.js"></script> <script src="Script.js"></script> <title></title> </head> <body> <table> <tr> <td colspan="2" style="width:600px; height:100px; background-color:#b4b4b4; text-align:center"> This is the header area </td> </tr> <tr> <td style="width:200px; height:400px; background-color:#ff0000"> <ul> <li><a href="#/home">Home</a></li> <li><a href="#/courses">Courses</a></li> <li><a href="#/student">Student</a></li> </ul> </td> <td style="width:400px; height:400px; background-color:#ff6a00"> <ng-view></ng-view> </td> </tr> <tr> <td colspan="2" style="width:500px; height:100px; background-color:#b4b4b4; text-align:center"> This is a Footer Area </td> </tr> </table> </body> </html> 

homeHTML, studentHTML, coursesHTML all are same as below.

<h1>{{message}}</h1>
<div>
    <p>Hi this is home partial view</p>
</div>

I found this error in the snippet part

Error:{
  "message": "Uncaught ReferenceError: angular is not defined",
  "filename": "http://stacksnippets.net/js",
  "lineno": 51,
  "colno": 13
}

What happened here is I get the view as shown in snippet with no errors in the browser and also when i click links home, courses, student nothing appears. Please point out what went wrong here.

Guyss I found out the answer

/// <reference path="Scripts/angular.min.js" />
var myApp = angular.module("myModule", ["ngRoute"])
                    .config(function ($routeProvider, $locationProvider) {
                        $locationProvider.hashPrefix('');
                        $routeProvider
                            .when("/home", {
                                templateUrl: "homeHTML.html",
                                controller: "homeController"
                            })
                            .when("/courses", {
                                templateUrl: "coursesHTML.html",
                                controller: "coursesController"
                            })
                            .when("/student", {
                                templateUrl: "studentHTML.html",
                                controller: "studentController"
                            })
                    })
                    .controller("homeController", function ($scope) {
                        $scope.message = "Home";
                    })
                    .controller("coursesController", function ($scope) {
                        $scope.message = "Courses";
                    })
                    .controller("studentController", function ($scope) {
                        $scope.message = "Student";
                    });

I have added $locationProvider. I will update the reason soon.

First define angular module in your html or body tag and make sure your angular version is same as angular-router version.Also don't forget to define respective controllers in homeHTML.html, coursesHTML.html and studentHTML.html

homeHTML.html

<div ng-controller="homeController">

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

</div>

Its working for me.

check out this link : https://plnkr.co/edit/cZzk8EKBXqRuoy6oYhuF?p=preview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM