Reputation: 61
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 .
Upvotes: 3
Views: 2468
Reputation: 380
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');
}]);
Upvotes: 0
Reputation: 816
Try using:
<div class="container">
<ui-view></ui-view>
</div>
// instead of:
<div class="container" ui-view="" ></div>
Upvotes: 2
Reputation: 134
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');
Upvotes: 0
Reputation: 7194
If you are passing straight HTML to use as the template you need to use template:
, not templateUrl:
. Here are the docs on templates.
Upvotes: 1