Search code examples
angularjsng-viewangularjs-ng-routelogin-page

why ng-view doesn't display anything in my angularjs code


I'm new to AngularJS and I tried creating a sample login page and I tried routing to other page on successful login but nothing shows up in ng-view and my code has no error. What could be the problem?

index.html

<html>
<head>
<script 
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-
route.js"></script>
<script src="maincontroller.js"></script>    
</head>
<body>
<div ng-view>
</div>
</body>
</html>

controller

var app = angular.module('myapp', ['ngRoute']);
app.config(function($routeProvider) {
$routeProvider
     .when('/', {
        templateUrl: 'login.html',
        controller: 'maincontroller'
    })
    .when('/dashboard', {
        templateUrl: 'dashboard.html'
    })
    .otherwise('/', {
        templateUrl: '/'
    })
    });
 app.controller('maincontroller', function($scope, $location) {
 $scope.submit = function($scope) {
 var username = $scope.username;
 var password = $scope.password;
if ($scope.username == 'ashok' && $scope.password == 'ashok') {
        $location.path('/dashboard');
    } else {
        windows.alert('wrong stuff');
}
};
});

login.html

<div ng-controller="maincontrol">


<form action="/" name="formgroup">
    <label>Username:<input type="text" id="username" ng-model="username"/></label><br>
    <label>Password:<input type="password" id="password" ng-model="password"/></label><br>
    <button type="button" ng-click="submit()">Login</button>
</form>


Solution

  • You should mention ng-app in your HTML to make this an Angular app.

    <html ng-app='myapp'>
    
    <head>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">
      </script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-
    route.js"></script>
      <script src="script.js"></script>
    </head>
    
    <body>
      <div ng-view>
      </div>
    </body>
    
    </html>
    

    Maincontroller.js

    var app = angular.module('myapp', ['ngRoute']);
    app.config(function($routeProvider) {
      $routeProvider
        .when('/', {
          templateUrl: 'login.html',
          controller: 'maincontroller'
        })
        .when('/dashboard', {
          templateUrl: 'dashboard.html'
        })
        .otherwise('/', {
          templateUrl: '/'
        })
    });
    app.controller('maincontroller', function($scope, $location) {
      $scope.submit = function() {
        var username = $scope.username;
        var password = $scope.password;
        if ($scope.username == 'ashok' && $scope.password == 'ashok') {
          $location.path('/dashboard');
        } else {
          windows.alert('wrong stuff');
        }
      };
    });
    

    $scope is already injected in to the controller and you are passing that as a parameter to your submit function which is undefined since you did not pass anything on submit.

    Login.html

    <form action="/" name="formgroup">
        <label>Username:<input type="text" id="username" ng-model="username"/></label><br>
        <label>Password:<input type="password" id="password" ng-model="password"/></label><br>
        <button type="button" ng-click="submit()">Login</button>
    </form>
    

    Since you are injecting controller on routing, You don't have to use ng-controller in your login.html. It makes the controller execute again.

    Check this Plunker: https://plnkr.co/edit/8jPJ7WOa3ixjqeRU8bpg?p=preview