如何激活状态并将对象传递给新视图?

时间:2015-06-30 11:32:43

标签: angularjs angular-ui-router

app.js:

 $stateProvider.state('datasource', {
            url: '/datasource',
            templateUrl: 'views/datasource.html',
            controller: 'datasourceController'
        });

        $stateProvider.state('datasourceList', {
            url: '/datasource/:datasourceId',
            templateUrl: 'views/datasource.list.html',
            controller: 'datasourceController'
        });

然后,我在(datasourceController)

中定义了一个onClick激活的函数
$scope.submitSelected = function() {
    if (angular.isDefined($scope.selectedSource)) {
        $scope.datasource = dataSourcesService.find($scope.selectedSource.id);
        $state.go('datasourceList', {datasourceId : datasource});
    }
 }

正如我所说,在我的datasource.html视图中有一个点击元素,触发submitSelected()函数

在函数中调用时,我可以根据ID从我的服务中成功检索数据。

问题是,在更改状态后,我无法在视图datasource.list.html中访问它的结果(范围)。

1 个答案:

答案 0 :(得分:0)

添加' param'在州配置

$stateProvider.state('datasource', {
        url: '/datasource',
        templateUrl: 'views/datasource.html',
        controller: 'datasourceController'
    });

    $stateProvider.state('datasourceList', {
        url: '/datasource/:datasourceId',
        templateUrl: 'views/datasource.list.html',
         params:      ['index', 'anotherKey'],
        controller: 'datasourceController'
    });

使用另一个控制器中的$ stateParams获取值

 app.controller('datasourceController', function($scope, $stateParams) {
var index = $stateParams.index;
var anotherKey = $stateParams.anotherKey;

});

相关问题