0

I was going through some tutorial on Angular JS and I can load single page and do things within single file, but when I got to the routing, it would not get to the other page.

Here's what I have in the app.js:

var app = angular.module('tutorialApp', ["ngRoute", "tutorialCtrlModule"]);

app.config(function($routeProvider) {
    $routeProvider

        .when("/", {
            templateUrl: "views/tutorial.html",
            controller: "TutorialCtrl"
        })
        .when("/tutorialSecond", {
            templateUrl: "views/tutorialSecond.html",
            controller: "TutorialCtrl2"
        })
        .otherwise({
            redirectTo: "/"
        })

})

and in my index.html, I include these script files:

<script src="lib/angular.min.js"></script>
<script src="lib/angular-route.min.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers/tutorialCtrl.js"></script>

and just have this in the body:

<body>
<div ng-view></div>
</body>

And in my tutorial.html, I have a link defined like this:

<a href="#/tutorialSecond">Tutorial Second Page</a>

So I can see the contents of the tutorial.html when I load my index.html. But when I click on the "Tutorial Second Page" link, nothing happens. And I see the URL/URI of something like this:

file:///<some_path>/AngularTutorial/index.html#!/#%2FtutorialSecond

If I change the app.js code to something like this:

.when("/", {
    templateUrl: "views/tutorialSecond.html",
    controller: "TutorialCtrl2"
})

I can see the tutorialSecond.html content show up when I load my index.html.

So why is the link to /tutorialSecond not loading from my original code when I click on the link to it? Is it because I am running and opening the index.html as a file://<path_to_index.html> locally and not running on a web server?

Please help... this is blocking me to continue with the tutorial. I am running in Windows 7x64, using Firefox, and just downloaded the latest available angular js files yesterday.

Thank you in advance.


Adding some additional details per some of the responses...

In my tutorialCtrl.js file, both controllers are defined something like this:

angular.module("tutorialCtrlModule", [])

.controller("TutorialCtrl", ["$scope", function($scope) {
    $scope.tutorialObject = {};
    $scope.tutorialObject.title = "Main Page";
    $scope.tutorialObject.subTitle = "Sub title";
    }
}])

.controller("TutorialCtrl2", ["$scope", function($scope) {
    $scope.secondTutorial = "This is the second tutorial";
}]);

and this .js file is included in the index.html as shown above.

2 Answers 2

0

Did you create and include script file related to tutorialCtrl2 in the index.html?

<script src="js/controllers/tutorialCtrl2.js"></script>
Sign up to request clarification or add additional context in comments.

1 Comment

I've updated my question with this info, but basically a single tutorialCtrl.js defines both TutorialCtrl and TutorialCtrl2 controllers. Please see the bottom of my question for this.
0

Your index.html didn't include script tutorialCtrl2.js and also you can use $locationProvider.html5Mode(true) to tell angular to use HTML5 strategy if available.

Here the list of browser that support HTML5 strategy: http://caniuse.com/#feat=history

5 Comments

I've updated my question with this info, but basically a single tutorialCtrl.js defines both TutorialCtrl and TutorialCtrl2 controllers. Please see the bottom of my question for this.
On this locationProvider stuff.. I am guessing I update my app.js to have a line like this? app.config(function($routeProvider, $locationProvider) { Then put that line you mentioned? $locationProvider.html5Mode(true)
I probably did things wrong, but just doing what I mentioned above ends with an error and I see this in the debugging console: Error: [$location:nobase] http://errors.angularjs.org/1.7.2/$location/nobase
If you configure $location to use html5Mode, you need to specify the base URL for the application with a <base href=""> tag or configure $locationProvider to not require a base tag by passing a definition object with requireBase:false to $locationProvider.html5Mode(): Remember that if you want to use $locationProvider you have to inject it in your config function
Here's an example $locationProvider configuration: $locationProvider.html5Mode({ enabled: true, requireBase: false });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.