2013-05-08 50 views
1

我正在試驗angular.js和require.js。我想要做的是一個簡單的登錄表單模塊。我的項目基於https://github.com/partap/angular-requirejs-seed項目。angular.js和require.js - 如何導航?

所以,我的路線:

angular.module('app', []) 
    .config([ '$routeProvider', 
     function ($routeProvider) { 
      $routeProvider.when('/auth', { 
       templateUrl : 'forms/auth.html', 
       controller : ... 
      }); 
      $routeProvider.when('/account', { 
       templateUrl : 'forms/account.html', 
       controller : ... 
      }); 
      $routeProvider.otherwise({ redirectTo : '/auth' }); 
     }]); 

所以,當應用程序啓動時,它導航到#/ AUTH。沒關係。 - 我認爲我應該以某種方式使用$位置變量,但不知道如何得到它

define([ 'angular' ], function (angular) { 
    return function ($scope) { 
     ... do something here ... 
     ... and redirect to /account if credentials are valid ... 
    }; 
}); 

一切順利,直到重定向:在auth控制器如下創建。

回答

0

你需要在把它作爲一個依賴於模塊

define([ 'angular' ], function (angular) { 
    return app.controller('yourController', ['$scope','$location', function ($scope, $location) { 
     ... do something here ... 
     $location.path('/account') 
    }]); 
}); 
+0

謝謝!它適用於app.controller返回應用程序本身的唯一更正。但不幸的是$ location.path('/ account')什麼都不做......這是另一個問題,但也許我錯過了一些微不足道的東西? – 2013-05-08 21:03:44

+1

你可以看到[這個SO帖子](http://stackoverflow.com/questions/11907961/redirect-using-angularjs)可能的重定向問題。在您的路由器中設置斷點,查看您的URL是否更新,查找控制檯錯誤等。 – 2013-05-08 21:41:19

+0

可能對任何前來指出您必須如何解決問題的人有用。 – 2013-05-09 02:23:50