0

I have been trying to navigate to views using angulateJs ngRoute. I defined first navigation using

$routeProvider.
            when('/login', {
                templateUrl: 'login.html',
                controller: 'loginCtrl'

            }).
            when('/home', {
                templateUrl: 'home.html'
            }).
            otherwise({
                redirectTo: '/login'
            });

Now I want to move to page home from loginController. How can I achieve that. every post just saying about navigating from main page only. I tried with

app.controller('loginCtrl', function($scope, $location){
    $scope.login = function(){
        $location.path('/home');
    }
});
Binod Singh
  • 682
  • 4
  • 11
  • 22
  • app.controller('LoginCtrl', function($scope, $location){ $scope.login = function(){ $location.path('/home'); } }); Its not working – Binod Singh Feb 26 '15 at 08:04

4 Answers4

1

Binod you should probably look in to the $state injector on the controller. This $state will have states to which you can reach, which are internally mapped to some other templates, like $state.go('someOtherPage'). I would suggest you to checkout $stateProvider

Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
0

Inject $location in your code where u want to go to the template, and then use $location.path('/home');

KaustubhSV
  • 328
  • 4
  • 15
0

try

       when('/home',{
                templateUrl : 'home.html',
                controller : 'HomeController'
       })

and create one empty controller with the name HomeController.

Shweta M
  • 146
  • 1
  • 2
  • 11
0

Solved using $stateProvider.

var routerApp = angular.module('starter', ['ui.router','']);

routerApp.config(function($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/login');

$stateProvider

    .state('login', {
        url: '/login',
        templateUrl: 'login.html',
        controller: 'loginCtrl'
    })

    .state('home', {
        url: '/homeffgfg',
        templateUrl: 'home.html'  
    });

});

and Login Controller is

app.controller('loginCtrl', function($scope, $state){
$scope.login = function(){
    $state.go('home');
}

});

ui.router dependency is important and <script type="text/javascript" src="js/angular-ui-router.js"></script>

Binod Singh
  • 682
  • 4
  • 11
  • 22