0

I'm trying to use a ui-router in Angular to generate a single page with multiple templates in <ui-view></ui-view> but it is unable to load a single template. It just gives a blank page with no JavaScript errors, even the nav bar does not appear.

I have gone through many questions about this on stack but even applying their solutions did not help:

Here is my index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<!--stylesheets-->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="assets/css/normalize.css">
    <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="assets/css/owl.css">
    <link rel="stylesheet" type="text/css" href="assets/css/animate.css">
    <link rel="stylesheet" type="text/css" href="assets/fonts/font-awesome-4.1.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="assets/fonts/eleganticons/et-icons.css">
    <link rel="stylesheet" type="text/css" href="assets/css/cardio.css">
    <link rel="stylesheet" type="text/css" href="assets/css/bootstrap-theme.min.css">
</head>
<body ng-app="myShelfApp">
    <nav class="navbar">
        <div class="container" >
            <!-- Brand and toggle get grouped for better mobile display -->
            <!-- <div class="navbar-header">
                <button type="button" class="navbar-toggle " data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#"><img src="assets/img/logo.png" data-active-url="assets/img/logo-active.png" alt=""></a>
            </div> -->
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right main-nav">
                    <li><a ui-sref="home">Home</a></li>
                    <li><a ui-sref="devices">Search</a></li>
                    <li><a ui-sref="addDevice">Add</a></li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container-fluid -->
    </nav>

    <div ui-view></div>

    <section id="service" class="section section-padded">
    <!-- Holder for mobile navigation -->



    </section>
    <!-- scripts -->
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="assets/js/angular.js"></script>
      <script src="assets/js/angular-ui-router.js"></script>
      <script src="https://unpkg.com/ng-table@2.0.2/bundles/ng-table.min.js"></script>
      <script src="app.js"></script>
      <script src="service/device.service.js"></script>
      <script src="controller/main.controller.js"></script>
      <script src="controller/shelf.controller.js"></script>
      <script src="controller/device.controller.js"></script>
      <script src="controller/newdevice.controller.js"></script>

</body>
</html>

This is the app.js

(function (){
   "use strict";
    var app=angular.module("myShelfApp",['ui.router','ngTable',"deviceservice"]);


    // app.service('DeviceFactoryService',deviceservice(DeviceFactory));
    app.config(['$stateProvider', '$urlRouterProvider', function   ($stateProvider,$urlRouterProvider){
          $urlRouterProvider.otherwise("/");

        $stateProvider
        .state("home",{
            url:"/",
            templateUrl:"main.html",

        });
    }]);

    app.run(function($rootScope) {
     $rootScope.$on("$stateChangeError", console.log.bind(console));
    });

})();

and here are my templates:

    <div id="home" ng-Controller="mainController">
        <div class="container" >
            <div class="row text-center title">
                <!--<h2>Services</h2>
                <h4 class="light muted">Achieve the best results with our wide variety of training options!</h4>-->
                <div class="jumbotron">
                  <h1 class="display-3"> Device Farm </h1>
                  <hr class="my-4">
                  <p><span class="label label-info">{{count}}</span></p>
                </div>
            </div>
            <div class="row services">
                <div class="col-md-4">
                    <a href="/devices" >
                    <div class="service">
                        <div class="icon-holder">
                            <img src="assets/img/icons/phone-blue.png" alt="" class="icon">
                        </div>
                        <h3 class="description">My Devices</h3>
                    </div>
                    </a>
                </div>
                <div class="col-sm-4">
                <a href="/add_device">
                    <div class="service">
                        <div class="icon-holder">
                            <img src="assets/img/icons/add-blue.png" alt="" class="icon">
                        </div>
                        <h3 class="description">Add Device</h3>
                    </div>
                </a>
                </div>
                <div class="col-md-4">
                    <a href="">
                    <div class="service">
                        <div class="icon-holder">
                            <img src="assets/img/icons/search-blue.png" alt="" class="icon">
                        </div>
                        <h3 class="description">Search Device</h3>
                    </div></a>
                </div>
            </div>
        </div>
        <div ></div>
    </div>

And this is my app.index.js where i m redirecting to index.js

    var express=require("express");
    var app= express();
    var bodyParser = require('body-parser');    
    var methodOverride = require('method-override');
    var router=require('./app.routes.js');
    var port=8888;

    app.use(express.static(__dirname + '/public'));  
    app.use(bodyParser.urlencoded({'extended':'true'}));
    app.use(bodyParser.json());
    app.use(methodOverride());
    app.use((request,response,next)=>{
    console.log(request.headers)
    next()
    });
    app.use('/api',router);
    app.get('*',function(req,res){
    res.sendFile('index.html',{root:__dirname+'/public'});
    });
    app.listen(port,error);
    function error(err){
    if(err){
        console.log(`Something went wrong`,err);
    }

    console.log(`Server is running on ${port}`);
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
  • I haven't taken the time to recreate your code, but the first thing that stands out immediately is `$urlRouterProvider.otherwise("main.html");`... your otherwise should load a URL fragment, not a template. also, your default state should be `"/"`. not `""`. change both of those to `"/"` and see if anything changes. – Claies Jan 21 '17 at 13:13
  • @Claies did that change but didnt help still the same problem – divyemarwah Jan 21 '17 at 13:21
  • Try moving ``before `` – Alon Eitan Jan 21 '17 at 13:24
  • @AlonEitan So i tried that but it didnt help , anyway that would throw a problem with controllers and not loading of the templates – divyemarwah Jan 21 '17 at 13:35
  • @user3488376 Please include the errors you're getting in the console. The nature of the issue is still unclear. You should also search for 404 errors (Or other errors) in the network tab on Google Chrome developer tools – Alon Eitan Jan 21 '17 at 13:37
  • I'm getting no errors in console – divyemarwah Jan 21 '17 at 14:15
  • Here is a plunker using your code: https://plnkr.co/edit/OChE46huC6azJc00esMS?p=preview. Try to reproduce your issue there and then we can take a look – Vivek Athalye Jan 21 '17 at 16:19

1 Answers1

0

So I guys i found the reason why my ui-router wasnt working. Apparently you cant declare app.module('app',['dependency']) even in your controllers or other modules with angular.modular('app',[]) also will override your main app.module and because of this i the control wasnt entering app.config for the ui-router to work.

refer to answer :

Angular routing config function not called for further understanding . Thank you guys

Community
  • 1
  • 1