1

我正在试验 angular.js 和 require.js。我想做的是一个简单的登录表单模块。我的项目基于https://github.com/partap/angular-requirejs-seed项目。

所以,我有路线:

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. 没关系。auth 控制器创建如下:

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

在重定向之前一切顺利 - 我认为我应该以某种方式使用 $location 变量,但不知道如何获取它。

4

1 回答 1

0

您需要将其作为依赖项传递给模块

define([ 'angular' ], function (angular) {
    return app.controller('yourController', ['$scope','$location', function ($scope, $location) {
        ... do something here ...
        $location.path('/account')
    }]);
});
于 2013-05-08T20:42:05.883 回答