September 9, 2015

AngularJs routing not working

I started learning AngularJS, and faced problem that routing is not getting worked. It only displays my index page and I was unable to display my partial views. Even it was not showing any error message while try to trace using firebug.

These are the points you have to consider if you get stuck with Angular Routes not working:

1- While going to define your config for routing, remember to put 'ngRoute' dependency parameter.
For example if following is your code snippet:
var myApp = angular.module('myApp');
{
    $routeProvider
    .when('/view1',
    {
        controller: 'myController',
        templateUrl: 'view1.html'
    })
    //code goes on...
}

You have to change it by putting 'ngRoute' dependency.
var myApp = angular.module('myApp', ['ngRoute']);
{
    $routeProvider
    .when('/view1',
    {
        controller: 'myController',
        templateUrl: 'view1.html'
    })
    //code goes on...
}
2- You may be started Angular JS demos by referencing angular.js library. Note that for routing get worked, we have to reference another library angular-route.js (http://code.angularjs.org/1.2.13/angular-route.js)

3- It should work now. If still not work, try to run your application by hosting in a webserver. For example try run it through Visual Studio

No comments:

Post a Comment