使用究竟导航离子意见/控制器之间的数据 [英] What to use exactly to navigate data between views/controllers in ionic

查看:141
本文介绍了使用究竟导航离子意见/控制器之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始开发一个简单的应用程序:

I've started developing a simple application :

在我retreiving所有游戏第一种观点,而另一种观点我正在根据游戏ID retreivig的游戏细节。

in the first view I'm retreiving all the games, and the other view i'm retreivig the game details according to the game id.

我没有链接的两页呢。

这是我现在面临的问题。我很困惑 !我应该使用离子看法?或者我应该使用一个普通页

this is what i'm facing as problem. i'm confused ! should I use ion-view ?? or I should use a normal page

为每个视图我有一个控制器,它看起来就像:

for each view I have a controller which look almost like :

.controller('tomorrowmatches', function($scope, $http) {
    $http.get("http://www.myappbackend/ofc/matches?date=2015-05-03")
        .success(function (response) {
            $scope.matches = response;

            }

        });

})

和如何从电脑板数据传递到另一个,在我的例子我想通过game.id作为截图shwon。

and how to pass data from conroller to another, in my example I wanna pass the game.id as shwon on the screenshot.

如果您需要更多的细节只是让我知道。我只是需要有人来把事情说清楚对我来说,如果有一个例子那就太棒了。

if you need more details just let me know. I just need someone to make things clear for me, and if there is an example it would be fantastic.

推荐答案

将数据传递到另一个视图,你可以使用$状态和$ stateParams服务。

To pass data to another view you can use the $state and $stateParams services.

示例

控制器1 (发送数据)

.controller('MyCtrl1', function($scope, $state) {
        $scope.selectedData = function(selectedId) {
            $state.go('yourState', { id: selectedId });
        };
    })

2控制器(获取数据)

.controller('YourCtrl', function($scope, $state, $stateParams) {
        if ($stateParams.id) {
            $scope.yourParam = $stateParams.id;
        }

        // Do anything you want with the ID inside $scope.yourParam
    })

app.js

.config(function($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('init', {
                url: "/init",
                templateUrl: "templates/init.html",
                controller: "MyCtrl1"
            })
            .state('yourState', {
                url: "/yourState?id",
                templateUrl: "templates/your-template.html",
                controller: "YourCtrl"
            })

        // if none of the above states are matched, use this as the fallback
        $urlRouterProvider.otherwise('/init');
    })

正如您可以在状态看 yourState 我分配了一个 ID 作为参数。该参数如果 YourCtrl 存在,检查是否存在分配到的范围,然后做任何你想用它。

As you can see in the state yourState I assigned an id as a parameter. This parameter will be checked if exists by YourCtrl, if it exists assign to scope and then do whatever you want with it.

记住要设置的参数选项在app.js路由配置。

Remember to set the parameter options in your app.js route configuration.

检查 UI路由器文档关于更多的相关信息。你有更多的方式来发送数据。

Check the ui-router docs for more info on this. You have more ways to send data.

这篇关于使用究竟导航离子意见/控制器之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆