如何在AngularJS中将数据从一个视图传递到另一个视图

时间:2017-05-05 11:19:37

标签: angularjs redirect view angular-ui-router

我正在从view1点击按钮传递数据,现在我希望将这些数据传递到另一个视图(view2)。我在按钮点击时调用另一个状态。请说明如何传递数据?

$scope.getSample=function(){
                $scope.spreadsheet=$scope.SpreadsheetLists.name;
                $scope.worksheet=$scope.SheetList.properties.title;
                $state.go('view2');
            };

我不想使用状态参数,如下所示:

  $state.go('view2',key:$scope.worksheet);

2 个答案:

答案 0 :(得分:2)

使用$ rootScope.data =“some data”;并在其他控制器中检索。或创建工厂方法并通过工厂传递值。

答案 1 :(得分:0)

以下是使用$ state

传递数据和URL的代码
$stateProvider
  .state('view2', {
  url: "/view2",
  templateUrl: 'tpl.html',
  params: { name: null }
})

$state.go("view2", {                
    name: "Dummy Name"
});

阅读数据的方式

.controller('view2Ctrl', function ($stateParams, $scope) {       
    console.log($stateParams.name);
});

没有其他可用方法。

相关问题