0

i'm having this online course on AngularJS, which provides the code in folders, according to the lesson. This one is about routing and single page applications; similar demos and structures work on plunker (like this one ) but not in my machine...I don't understand why, can someone help me out?

EDIT: actually, if I download this demo the thing wont work...

INDEX file:

<html lang="en-us" ng-app="myApp">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <meta charset="UTF-8">


        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
        <style>
            html, body, input, select, textarea
            {
                font-size: 1.05em;
            }
        </style>


        <script src="//code.angularjs.org/1.3.0-rc.1/angular.min.js"></script>
        <script src="//code.angularjs.org/1.3.0-rc.1/angular-route.min.js"></script>
        <script src="app.js"></script>
    </head>
    <body>

        <header>
            <nav class="navbar navbar-default">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand" href="/">AngularJS</a>
                </div>

                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
                    <li><a href="#/second"><i></i> Second</a></li>
                </ul>
            </div>
            </nav>
        </header>

        <div class="container">

            <div ng-view></div>

        </div>

    </body>
</html>

The Module file: APP.JS

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

myApp.config(function ($routeProvider) {

    $routeProvider

    .when('/', {
        templateUrl: 'pages/main.html',
        controller: 'mainController'
    })

    .when('/second', {
        templateUrl: 'pages/second.html',
        controller: 'secondController'
    })

    .when('/second/:num', {
        templateUrl: 'pages/second.html',
        controller: 'secondController'
    })

});

myApp.controller('mainController', ['$scope', '$log', function($scope, $log) {

    $scope.name = 'Main';

}]);

myApp.controller('secondController', ['$scope', '$log', '$routeParams', function($scope, $log, $routeParams) {

    $scope.num = $routeParams.num || 1;

}]);

Pages one and two (views):

<h1>This is Main.</h1>
<h3>Scope value: {{ name }}</h3>

<h1>This is second.</h1>
<h3>Scope route value (on second page): {{ num }}</h3>

The folder structure. The code is as suggested enter image description here

6
  • Your references for angular is correct? Commented Aug 1, 2017 at 10:05
  • Are u getting any error in console Commented Aug 1, 2017 at 10:06
  • well, yes, it says "angular is not defined" first line on app.js Commented Aug 1, 2017 at 10:10
  • I dont get it because I have other samples - not about routing - and they work.... Commented Aug 1, 2017 at 10:12
  • Do they have the same script source as above? Try changing the script tags as below Commented Aug 1, 2017 at 10:14

3 Answers 3

1

Your references for angular and bootstrap are not correct

Angularjs

<script src="https://code.angularjs.org/1.3.0-rc.1/angular.min.js"></script>
<script src="https://code.angularjs.org/1.3.0-rc.1/angular-route.min.js"></script>

Bootstrap

<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
Sign up to request clarification or add additional context in comments.

7 Comments

Bootstrap now loads, but the links dont work. Here's the log:Error: [$compile:tpload] http://errors.angularjs.org/1.3.0-rc.1/$compile/tpload?p0=pages%2Fmain.html at angular.js:38 at g (angular.js:14932) at angular.js:12076 at m.$eval (angular.js:13276) at m.$digest (angular.js:13088) at m.$apply (angular.js:13380) at HTMLHtmlElement.<anonymous> (angular.js:10414) at HTMLHtmlElement.c (angular.js:2977)
It is telling that it failed to fetch the templates that u have defined in your routing. Are the paths correct?
I guess so, I haven't changed the file structure. Besides, as I said above, if I build a simple module and controller (two files), the thing works fine, so I guess the problem isnt the CDN
Do you have the html files in pages directory which is in the same level as your app.js?
yes, I'm sorry, for the late reply. The folder was supose to work as it is; changing the links only gave me bootstrap...As for Angular, if i write some basic code like an object attached to scope and then render it, the thing works...so i'm confused
|
0

You need to correct you urls like this

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

myApp.config(function ($routeProvider) {

    $routeProvider

    .when('/', {
        templateUrl: 'pages/main.html',
        controller: 'mainController'
    })

    .when('/second', {
        templateUrl: 'pages/second.html',
        controller: 'secondController'
    })

    .when('/second/:num', {
        templateUrl: 'pages/second.html',
        controller: 'secondController'
    })

});

myApp.controller('mainController', ['$scope', '$log', function($scope, $log) {

    $scope.name = 'Main';

}]);

myApp.controller('secondController', ['$scope', '$log', '$routeParams', function($scope, $log, $routeParams) {

    $scope.num = $routeParams.num || 1;

}]);
<html lang="en-us" ng-app="myApp">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <meta charset="UTF-8">


        <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
        <style>
            html, body, input, select, textarea
            {
                font-size: 1.05em;
            }
        </style>


        <script src="https://code.angularjs.org/1.3.0-rc.1/angular.min.js"></script>
        <script src="https://code.angularjs.org/1.3.0-rc.1/angular-route.min.js"></script>
        <script src="app.js"></script>
    </head>
    <body>

        <header>
            <nav class="navbar navbar-default">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand" href="/">AngularJS</a>
                </div>

                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
                    <li><a href="#/second"><i></i> Second</a></li>
                </ul>
            </div>
            </nav>
        </header>

        <div class="container">

            <div ng-view></div>

        </div>

    </body>
</html>

Also you need to add a page for the view on the following path in project pages/main.html

enter image description here

2 Comments

? honnestly I don't understand you answer. The code and structure are exacly the same...
I have added https in all links you have used in your header
0

You need to use your application throught the localhost like this:

enter image description here

Just mount your project on HTTP server.

$> sudo npm -g live-server
$> cd path/to/root/dir/project
$> live-server

EDIT: and eventually, include every time your scripts at bottom of body:

<body>

    .....
    .....
    .....

    <script src="//code.angularjs.org/1.3.0-rc.1/angular.min.js"></script>
    <script src="//code.angularjs.org/1.3.0-rc.1/angular-route.min.js"></script>
    <script src="app.js"></script>
</body>

Comments

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.