2016-04-17 88 views
2

我试图按照this tutorial但使用Node.js的在AngularJS UI路由器,状态改变,网址改变,但模板URL不是

的问题是,当我点击登录按钮,它是从服务器端授权,该网址更改为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> 

我已经包括角和UI路由器源到头部,并尝试添加ui-view到venue.html中,但它不会以某种方式加载。

请帮帮忙,我在AngularJS新的,它是需要尽快完成

谢谢!

回答

1

如果您没有stateParams通过,则不需要$state.go中的第二个参数。下面改用:

$state.go('venue'); 

而且,问题是,你没有任何ui-view指令中,你的状态将会加载。添加ui-view指令在HTML这样的,它会工作

<div ui-view></div> 

了解更多关于ui-view这里:https://github.com/angular-ui/ui-router/wiki#where-does-the-template-get-inserted

+0

它的工作原理!非常感谢 –

相关问题