angularjs位置范围

前端之家收集整理的这篇文章主要介绍了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 
        }
}

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

任何的想法 ?

解决方法

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

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

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

在$routeProvider中配置路由:

$routeProvider.when('/account',{
    template: 'account.html',controller: AccountCntl  
});

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

function AccountCtrl($scope,$rootScope) {
    $scope.accountInfo = $rootScope.accountInfo;
}

猜你在找的Angularjs相关文章