Search code examples
javascriptangularjsangularjs-routing

ngRoute not working in Angular?


Here's the main index: I have two .php files which needs to be viewed in template.(ng-view) mainFrame.php

    <!DOCTYPE html>
    <html lang="en-US" ng-app="app">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title>Document</title>
        <link href="../css/materialize.min.css" rel="stylesheet">
        <link href="css/blogmain.css" rel="stylesheet"/>
        <link href='https://fonts.googleapis.com/css?family=Roboto:400,500,700italic,900|Lato|Tangerine|Montserrat|Robot
        o+Condensed:400,300italic,700italic' rel='stylesheet' type='text/css'>
        <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    </head>
    <body>
    <div class="row">
        <div class="navbar-fixed">
            <nav style="height: 65px">
                <div class="nav-wrapper">
                    <a href="#" class="brand-logo center" style="font-family:'Montserrat',serif;">
                        Welcome To </a>
                    <ul id="slide-out" class="side-nav">
                        <li class="waves-effect wifull"><a href="#/create" id="changeDiv">Create a
                                Post</a>
                        </li>
                        <li class="waves-effect wifull"><a href="#/posts" id="changeDiv1">View
                                Posts</a></li>
                    </ul>
                    <a href="#" data-activates="slide-out" class="button-collapse show-on-large show-on-medium-and-down"><i
                            class="mdi-navigation-menu"></i></a>
                </div>
            </nav>
        </div>
        <div ng-view></div>
    </div>
    <script src="../js/jquery-1.11.3.min.js"></script>
    <script src="../js/angular_min.js"></script>
    <script src="../js/angular_route.min.js"></script>
    <script src="js/appRoute.js"></script>
    <script src="../js/materialize.min.js"></script>
    <script>
        $(document).ready(function () {
            $('select').material_select();
        });
        $('.button-collapse').sideNav({
                menuWidth: 300,
                edge: 'left',
                closeOnClick: true
            }
        );
    </script>
    </body>
    </html>

.js file for routing.

appRoute.js

   (function () {
'use strict';
var app = angular.module('app', ['ngRoute']);
app.config(function ($routeProvider)
{
    $routeProvider
        .when('/create', {
            templateUrl: '../create/create.php',
            controller: 'CreateController'
        })
        .when('/posts', {
                templateUrl: '../viewPost/view_post.php',
                controller: 'ViewPostController'
            }
        )
        .otherwise({
            redirectTo: '/create'
        })
});

app.controller('CreateController', function ($scope) {
});

app.controller('ViewPostController', function ($scope) {
});

}).();

I don't know what's the problem but nothing is being shown in ng-view. Thanks.


Solution

  • Second variant of code :) Only put normal HTML templates

    'use strict';
    var app = angular.module('app', ['ngRoute']);
    app.config(['$locationProvider', '$routeProvider', 
     function($location, $routeProvider) {
        $routeProvider
            .when('/create', {
                templateUrl: '../create/create.php',
                controller: 'CreateController'
            })
            .when('/posts', {
                    templateUrl: '../viewPost/view_post.php',
                    controller: 'ViewPostController'
                }
            )
            .otherwise({
                redirectTo: '/create'
            })
    }]);
    
    app.controller('CreateController', function ($scope) {
    });
    
    app.controller('ViewPostController', function ($scope) {
    });