2016-06-22 18 views
0

私はユーザーに2人のロール、すなわち学生または図書館員がいるログインページを作成しています。 ユーザーが資格情報を入力してsubmitをクリックすると、validate関数が呼び出されます。今角度ルーティングが正常に動作していません

私が直面しています問題がある:ルーティングが動作していない

すなわち、制御が警告(「ログイン成功」)に達しました。 if($ scope.roles [i] .role === "student"){ が$ location.path( '/ home/student')にページを誘導しません。

ログインページのURLは以下の通りである:ログイン成功後に

http://localhost:63342/LabguideExamples/Assignment/Day4/Login.html?_ijt=oa5emr702cenehrs4oosarmg0d

URLは以下の通りです。

http://localhost:63342/LabguideExamples/Assignment/Day4/Login.html?_ijt=oa5emr702cenehrs4oosarmg0d#/home/student

URLはまだlogin.htmlとを持っており、それがapp.js.あたりとして、 'ViewBooks_Student.html' で置き換えられることはありません

ご迷惑をおかけして申し訳ありません。

おかげ

controller.js

var Controllers = angular.module('Controllers', ['ngRoute']); 
Controllers.controller('LoginCtrl', ['$scope','$http','$location', 
function ($scope,$http,$location) { 
    $scope.validate=function() 
    { 
     $http.get('data/roles.json').success(function(data) { 
      $scope.roles = data; 

     var count=0; 
      for (var i = 0, len = $scope.roles.length; i < len; i++) { 
if ($scope.username === $scope.roles[i].username && $scope.password === $scope.roles[i].password) { 
        alert("login successful"); 
        count = count + 1; 
        if ($scope.roles[i].role === "student") { 
         $location.path('/home/student'); 
         break; 
        } 
        else { 
         $location.path('/home/librarian'); 
         break; 
        } 

       } 
      } 

     if(count!=1) 
     { 
      alert("Please provide valid login credentials"); 
      $location.path("/main") 
     } 
     }); 
    } 

}]); 

app.js

var bookApp = angular.module('bookApp',[ 
'Controllers','ngRoute' 
]); 
bookApp.config(['$routeProvider', 
function($routeProvider) { 
    $routeProvider. 
    when('/main', { 
     templateUrl: 'Login', 
     controller: 'LoginCtrl' 
    }). 
    when('/home/student', { 
     templateUrl: 'ViewBooks_Student.html', 
     controller: 'BookListCtrl_Student' 
    }). 
    when('/home/librarian', { 
     templateUrl: 'ViewBooks_Librarian.html', 
     controller: 'BookListCtrl_Librarian' 
    }). 
    when('/issue/:bookId', { 
     templateUrl: 'IssueBook.html', 
     controller: 'IssueBookCtrl' 
    }). 
    when('/return/:bookId', { 
     templateUrl: 'ReturnBook.html', 
     controller: 'ReturnBookCtrl' 
    }). 
    otherwise({ 
     redirectTo: '/main' 
    }); 
}]); 
+1

'$ scope.role'はどこにも設定されていないので、その値は' undefined' – GillesC

+0

GillesCありがとう、私はそれをrole.roleに置き換えましたが、まだルーティングが動作していません。 –

+0

あなたの '$ http.get()'は非同期呼び出しですので、 '$ scope.roles'が読み込まれる前にコードが' angular.forEach() '行に進みます。おそらく '$ http.get()'コールの成功関数の中でそれらすべてを動かす必要があります。 – Lex

答えて

1

これは、深さの問題では一種のです。あなたがログイン後に理解する限り、あなたはUser Roleに基づくページにリダイレクトする必要があります。そう、あなたが角度の実行に記述する必要がある事は

angular.module('myapp').run([],function(){}) 

をブロックコントローラに書いた、この特定のロジックは基本的に角度ブロックは、コントローラの前に実行優先度の実行システムの実行を持っています。詳細については、Angular documentationを参照してください。 https://docs.angularjs.org/guide/module

関連する問題