简体   繁体   中英

$location.path('/dashboard'); not working

I am trying to create simple login app in angular js

following is my login.html

 <!DOCTYPE html> <html ng-app="AppName"> <head> <title></title> </head> <body> <div ng-controller="loginCtrl"> <form action="/" id="myLogin"> Username: <input type="text" id="username" ng-model="username"><br> Password: <input type="password" id="password" ng-model="password"><br> <button type="button" ng-click="submit()">login</button> </form> </div> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <script type="text/javascript" src="angular-route.min.js"></script> <script type="text/javascript" src="controller.js"></script> </body> </html> 

and following is my controller.js

 var app = angular.module('AppName', ['ngRoute']); app.config(function($routeProvider){ $routeProvider .when('/', { templateUrl: 'login.html' }) .when('/dashboard', { templateUrl: 'dashboard.html' }) .otherwise({ redirectTo: '/' }); }); app.controller('loginCtrl', function($scope, $location){ $scope.submit = function(){ var uname = $scope.username; var password = $scope.password; if ($scope.username == 'admin' && $scope.password == 'admin') { $location.path('/dashboard'); console.log($location.path()); } else { alert('wrong password'); } }; }); 

but after submitting it is not redirecting to dashboard.html. in console it is showing path as /dashboard only. i am new to angular.js.

没有要更改的视图占位符,您需要使用<div ng-view></div>

Use window.location = "/dashboard";

for loading your html templates you should use angular routing like below

var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
    templateUrl : "login.htm"
    ,controller : "LoginController"
})
.when("/dashbord", {
    templateUrl : "dashbord.htm"
    ,controller : "DashbordController"
})

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM