Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

ionic - angularjs Transfer data between pages

My English is very bad,at first Controller,I post data form The server,and i got a $rootScope.YD for Transfer data .but when i use the YD in the second page,it does't work.can you help me ?

.controller('yd_homeCtrl',function($rootScope, $scope, $state, $http,$ionicActionSheet, $ionicModal)
{
    $scope.getReadList = function ()
    {
        var url = $rootScope.rootUrl + "/read.php";
        var data = {
            "func":"getReadList",
            "unionid":$rootScope.userinfo.unionid,
            "fr":1
        };
        encode(data);

        $rootScope.LoadingShow;

        $http.post(url, data).success(function (response)
        {
            $rootScope.LoadingHide();
            $rootScope.YD=response.data.result[0];
            $state.go('yd_improve')
        }).error(function (response, status)
        {
            $rootScope.LoadingHide();
            $rootScope.Alert('连接失败![' + response + status + ']');
            return;
        });
    }
})

.controller("yd_improveCtrl",function($rootScope, $scope, $state, $http, $ionicActionSheet, $ionicModal, $stateParams, $interval, $sce, $ionicHistory,$ionicSlideBoxDelegate)
    {
    $scope.text="";
    angular.forEach($rootScope.YD,function(value,key){
        if(key==0)
        {
        //alert(1111111);
        //alert(value.text);
            $scope.text=value.text;
            alert($scope.text);
        }

    });

   });

there is app.js state:

 .state('yd_improve', {
    cache: false,
    url: '/yd_improve/:id',
    onExit: function ()
    {
        var v = document.getElementById("audio");
        v.pause();
        v.src = "";
    },
    templateUrl: 'templates/yd_improve.html',
    controller: 'yd_improveCtrl'
})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use $broadcast service. It's the most reliable solution for broadcasting events and passing parameters between controllers.

Don't rely on storing data in service because on page refresh, service also gets refreshed and you will lose the data stored in service's variable. So maintaining state in service is highly unrecommended. Use $broadcast instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...