2017-10-03 26 views
0

私は角度jを初めて使用しています。これが私の最初の基本的なステップです。私はアクセスするための簡単なログインページを作成しました。私が直面している問題は、login()ボタンをクリックしたときに、フォーム()が送信されず、無効なユーザー名とパスワードが表示されます。他のブログも検索しました。しかし、私は正しく理解していません。ログインページを作成するには、より良い方法です。

HTML::

<div class="package" ng-controller="credientials"> 
    <form ng-submit="loginform()" class="ng-scope ng-pristine ng-valid"> 
     <label form="emailinput">Email</label> 
     <input type="text" class="form-control ng-pristine ng-valid" ng-model="username"> 
     <label form="pwdinput">Password</label> 
     <input type="password" class="form-control ng-pristine ng-valid" ng-model="password"> 
    <div> 
     <button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button> 
     <button class="btn btn-primary" ng-click="">Login</button> 
    </div><br/> 
     <span class="text-danger">{{ error }}</span> 
    </form> 

角度JS:あなたはスコープにユーザ名とパスワードを配置する必要があり

var app = angular.module('logapp',['ngRoute']); 
app.config(function($routeProvider){ 
      $routeProvider 
       .when('/login',{ 
        templateUrl : "login.html", 
        controller : "loginctrl" 
       }) 
       .when('/home',{ 
        templateUrl : "home.html", 
        controller : "homectrl" 
       }); 
      $routeProvider.otherwise({ redirectTo: '/login'}); 
     }); 

app.controller('credientials',['$scope','$route','$http','$window',function($scope,$route,$http,$window){ 
    $scope.templates = 
       [ 
        { url: 'login.html' }, 
        { url: 'practice.html'} 
       ]; 
        $scope.template = $scope.templates[0]; 
        $scope.loginform = function (username, password) { 
       if (username === 'admin' && password === '1234') { 
        authentication.isAuthenticated = true; 
        $scope.template = $scope.templates[1]; 
        $scope.user = username; 
       } else { 
        $scope.error = "Invalid username and password"; 
       }; 
       }; 
     app.factory('authentication', function() { 
       return { 
       isAuthenticated: false, 
       user: null 
       } 
     }); 

    }]); 
+0

$ scope.username === 'admin' && $ scope.password === '1234' –

+0

@VolkanAkınPaşaを使用してください。私はちょうどあなたのコードを試しましたが、私は**認証**を持っていますが未定義です。 – SRK

+0

私は、その認証をその関数に渡します。しかし、私はまだエラーが発生しました – SRK

答えて

1

を検討すべきです。

app.controller('credientials',['$scope','$route','$http','$window',function($scope,$route,$http,$window,authentication){ 

は、私はあなたのコードhere

を変更している真のだろう、あなたのユーザ名とパスワード

$scope.username === 'admin' && $scope.password === '1234' 

の$スコープを使用する必要があり、その後、あなたは工場であなたの変数にisAuthenticatedが表示されます

0

は、以下の私のコードがあります。 多分これは良い仕事します:

$scope.username = ""; 
$scope.password = ""; 
$scope.loginform = function() { 
if ($scope.username === 'admin' && $scope.password === '1234') { 

あなたは本当にあなたのコントローラに工場「認証」を注入するのを忘れて、このチュートリアル
https://docs.angularjs.org/tutorial

関連する問題