angularjs位置范围

IxD*_*Day 4 angularjs

我对AngularJS范围和位置有一些问题.这是一个例子:

function CreateAccountCtrl($scope, $http, $location) {
    ...
    $http.post(url,$scope.form).
        success(function(data){
            $location.path('/'); // I need to transfert data to the location 
        }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:我想将数据传输到/ controller,我想使用rootScope,但我不认为这是最好的方法.

任何的想法 ?

Rod*_*ues 6

您可以使用$ rootScope在控制器之间发送数据.$ routeParams不允许发送复杂的数据结构.让我们来看看它.

将返回的成功回调数据分配给$ rootScope中的变量.导航到AccountController.

function CreateAccountCtrl($scope, $http, $location,$rootScope ) {
    ...
    $http.post(url,$scope.form).
        success(function(data){
            $rootScope.accountInfo = data; 
            $location.path('/account');
        }
}
Run Code Online (Sandbox Code Playgroud)

$ routeProvider中配置路由:

$routeProvider.when('/account', {
    template: 'account.html',
    controller: AccountCntl  
});
Run Code Online (Sandbox Code Playgroud)

在AccountController中可以访问$ rootScope上的数据.

function AccountCtrl($scope, $rootScope) {
    $scope.accountInfo = $rootScope.accountInfo;
}
Run Code Online (Sandbox Code Playgroud)