3

I have index.html which i am able to load but angular-ui-router is not routing it to proper templateURl (app.html is in same deirectoy as of index.html).I have added all the required script but it is not working

<!DOCTYPE html>
<html ng-app ="mustReadAlgo">
    <head>
        <title>AngularJS Basic Example</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="lib/css/bootstrap.min.css" />
        <link rel="stylesheet" href="lib/css/animate.min.css" />
        <script type='text/javascript' src="public/lib/moment.min.js" charset="UTF-8"></script>
        <script type='text/javascript' src="../../public/lib/lodash.min.js" charset="UTF-8"></script>
        <script type='text/javascript' src="../../public/lib/angular/angular.min.js" charset="UTF-8"></script>


        <script type='text/javascript' src="../../public/lib/angular-sanitize/angular-sanitize.min.js" charset="UTF-8"></script>
        <script type='text/javascript' src="../../public/lib/angular-ui-router/release/angular-ui-router.min.js" charset="UTF-8"></script>
        <script type='text/javascript' src="../../app.js" charset="UTF-8"></script>
        <script type='text/javascript' src="../../app/controllers/app.controller.js" charset="UTF-8"></script>





    </head>
    <body>

        <div class="container" ui-view="" ></div>


    </body>
</html> 

And my app.js is

    'use-strict';

    angular.module('mustReadAlgo',['ui.router']).config($stateProvider,$urlRouterProvider){

        $urlRouterProvider.otherwise('/');

        $stateProvider.state('home',{

            url :'/',
            templateUrl : 'app.html',(is in same directory as of index.html)
            controller :'mustReadAlgoCntrller'


        });



    }).run(function(){


    });

And controller code is in app.controller.js

angular.module('mustReadAlgo').controller('mustReadAlgoCntrller',function($scope)
{
    $scope.message='Hello World';
    console.log('inside controller');
});

Content of app.html

<div class="row">
  <div class="col-xs-2">
    <button class="form-control btn btn-default"><span class="glyphicon glyphicon-thumbs-up"></span>Hello World</button>
  </div>
    <p>Hello</p>
</div>

But ui routing is not working properly to load the templateUrl? Can anyone please help me out i am new to angular js .

10
  • That doesn't look like a URL. Commented Mar 23, 2016 at 21:39
  • Yea ,but it should work it is a html code so it should dispaly that.Even if i am using any url there it is not working.Say if i add template Url as 'app.html' which is at the same location where index.html is. Commented Mar 23, 2016 at 21:41
  • Do you get any errors? Maybe a typo in 'mustReadAlgoCntrller'? Commented Mar 23, 2016 at 21:47
  • Thanks for help .I dont see nay errors i checked that there is no typo.Adding controller code too in edit. Commented Mar 23, 2016 at 21:54
  • Why are you including both the regular and minimized versions of angular.ui.router.js? Also, maybe show the contents of app.html. Commented Mar 23, 2016 at 22:06

4 Answers 4

2

Try using:

<div class="container">
   <ui-view></ui-view>
</div>

// instead of:

<div class="container" ui-view="" ></div>
Sign up to request clarification or add additional context in comments.

1 Comment

At the definition of the view directive in the source code you can read restrict: 'ECA'. In the documentation is a reference for <ui-view/> when using abstract views.
1

If you are passing straight HTML to use as the template you need to use template:, not templateUrl:. Here are the docs on templates.

2 Comments

Thanks ,I will try that .Sorry i edited the question to use templateUrl.
I tried it with template to but that is not working as well.
0

You need to define a state for otherwise in the state configuration and the use the statename in the $urlRouterProvider.otherwise function

$stateProvider
    .state("otherwise", { url : '/'})

 $urlRouterProvider.otherwise('/otherwise');

Comments

0

Inject $state in controller, that would solve the problem. To make it clearer-

    angular.module('mustReadAlgo', ['ui.router']).controller('mustReadAlgoCntrller',['$scope', '$state', function($scope, $state)
{
    $scope.message='Hello World';
    console.log('inside controller');
}]);

1 Comment

How will this help resolve the issue? The OP is not using $state within the mustReadAlgoCntrller controller.

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.