我尝试关注this tutorial但使用Node.JS
问题是当我点击登录按钮时,它是从服务器端授权的,网址被更改为http://localhost:3000/#/venue但视图仍然相同,当我检查控制台中的状态时,它已经改变了到场地状态。
这是我的app.js代码
var myApp = angular.module('authApp', ['ui.router', 'satellizer']);
myApp.config(function($stateProvider, $urlRouterProvider, $authProvider) {
console.log("Hello World from module");
$authProvider.loginUrl = '/api/authenticate';
$urlRouterProvider.otherwise('/auth');
$stateProvider
.state('auth', {
url: '/auth',
templateUrl: '/index.html',
controller: 'AuthController'
})
.state('venue', {
url: '/venue',
templateUrl: 'venue.html',
controller: 'VenueController'
});
})
myApp.controller('AuthController',function($scope, $auth, $state) {
console.log("Hello World from auth controller");
console.log("current state1: ",$state);
$scope.login = function() {
var credentials = {
username: $scope.username,
password: $scope.password
}
$auth.login(credentials).then(function(data) {
$state.go('venue',{});
console.log("current state2: ",$state);
}, function(error) {
console.log(error);
});
}
});
myApp.controller('VenueController', function($scope, $auth, $state) {
console.log("Hello World from venue controller");
$scope.getVenues = function() {
$http.get('http://localhost:3000/api/venue/1').success(function(response) {
console.log("I got the data I requested");
$scope.users = response;
}).error(function(error) {
console.log('error');
});
}
});
这是我的登录页面(index.html)
<body ng-app="authApp" >
<div class="body"></div>
<div class="grad"></div>
<div class="header">
<div>Field Booking</div>
</div>
<br>
<div class="login" ng-controller="AuthController" >
<input type="text" placeholder="username" ng-model="username"><br>
<input type="password" placeholder="password" ng-model="password"><br>
<button ng-click="login()" > Login </button>
</div>
我已将angular和ui路由器源包含在标题中,并尝试将ui-view添加到venue.html中,但它不会以某种方式加载。
请帮助,我是AngularJS的新手,需要尽快完成
感谢!!!
答案 0 :(得分:1)
如果您没有传递stateParams
,则不需要$state.go
中的第二个参数。请改用以下代码:
$state.go('venue');
此外,问题是您没有任何ui-view
指令,您的状态将在其中加载。在您的html中添加ui-view
指令,它将起作用
<div ui-view></div>
在此处详细了解ui-view
:https://github.com/angular-ui/ui-router/wiki#where-does-the-template-get-inserted