2017-06-05 74 views
-1
//login.html 

<div ng-controller="loginController"> 
<form action="/" id="myLogin"> 
    User Name:<input type="text" ng-model="uname" /> <br /> 
    Password: <input type="password" ng-model="passwd"/> <br /> 
    <button type="button" ng-click="submit()">login</button> 
</form> 
</div> 

//controller.js 

var app = angular.module('myApp', ['ngRoute']); 

app.config(function($routeProvider) { 
    $routeProvider 

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

app.controller('loginController', function($scope, $location) { 
    $scope.submit = function() { 
     if ($scope.uname == 'admin' && $scope.passwd == 'admin') { 
      $location.path('employeeReg.html'); 
     } else { 
      alert('login failed'); 
     } 
    }; 
}); 

Attached Image不工作代碼角JS

我很新的角度JS,此示例代碼是隻需登錄頁面,但是當我打開索引頁沒有什麼是可見的,它應該重定向到登錄page.any幫助將受到歡迎。

+1

哪裏是你的索引頁的代碼? – NNR

+0

感謝您的回覆,實際上我已經添加了代碼的屏幕截圖,但它不顯示在這裏..https://i.stack.imgur.com/GBdze.png在此鏈接索引代碼可用。 – krish

+0

Where is the路由索引頁面在你的控制器內 – NNR

回答

0

嘗試包括角第一:

<script src='JS/angular.min.js'></script> 
<script src='JS/angular-route.min.js'></script> 
+0

已包括在內! – krish

+0

是的,但順序很重要。 – rrd

+0

嘗試過,仍然無法正常工作。 – krish

0

製造plunker。貌似工作

https://plnkr.co/edit/kHXK54?p=preview

var app = angular.module('myApp', ['ngRoute','ngSanitize']); 

app.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider) { 
     $routeProvider 
      .when('/', { 
     templateUrl: 'login.html', 
     controller : 'loginController' 
     }) 
     .when('/employee', { 
      templateUrl: 'employee.html' 
     }) 
     .otherwise({ 
      redirectTo:'/' 
     }) 
    }]); 

app.controller('loginController', function($scope, $location) { 
    $scope.submit = function() { 
     if ($scope.uname == 'admin' && $scope.passwd == 'admin') { 
      $location.path('/employee'); 
     } else { 
      alert('login failed'); 
     } 
    }; 
}); 
+0

感謝您的回覆,但沒有成功 – krish

+0

您是否檢查了該鏈接。它爲我工作。 – NNR

+0

是的,我檢查了,我認爲這是不同的情況下,在第一頁解析鏈接是有,但在我的情況下,我想要顯示登錄頁面時索引頁面打開 – krish