加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs位置范围

发布时间:2020-12-17 17:19:21 所属栏目:安全 来源:网络整理
导读:我对AngularJS范围和位置有一些问题.这是一个例子: function CreateAccountCtrl($scope,$http,$location) { ... $http.post(url,$scope.form). success(function(data){ $location.path('/'); // I need to transfert data to the location }} 我的问题是:
我对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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读