-1

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'
})
姚瑞卿
  • 3
  • 3
  • 1
    Create a service to transfer data between pages and share it with controllers http://stackoverflow.com/questions/22408790/angularjs-passing-data-between-pages – Nomi Ali Dec 19 '15 at 07:04
  • The rootScope is not saved between two different page reloads. Unless you are using angular routing. You need to post more of your code. – Simone Zandara Dec 19 '15 at 07:56
  • I have someone who write the old code,and i follow his code writing.. – 姚瑞卿 Dec 19 '15 at 08:24

2 Answers2

0

You have a couple of options. Create a service which stores the information. In first controller set the data. And in second controller get the data.

Or you can use $broadcast. This publishes an event with a name you give it and the data you parse through. You do this on first controller. And on second controller listen for the event using $rootScope.on.

But I'd recommend using a service its best solution in terms of memory and performance.

0

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.

  • thank you very much! but i new in ionic and angularjs. few days ago ,i was develop iOS by object-c. i haven't learn javascript before. so ,can you write a demo for me ? thank you very very much! – 姚瑞卿 Dec 19 '15 at 09:11
  • it works! i write the wrong {{param}},now it can display normal.anyway,thank you ! – 姚瑞卿 Dec 19 '15 at 09:32
  • No worries. Have your problem solved? If you want I can write an example /demofor you. Thanks – Mohammad Umair Khan Dec 19 '15 at 09:40
  • Yes,I just write wrong thing in my view......but there are more many questions waiting me solve. but in china,there are few people use Angularjs,there no person guide me , but i have confidence!thank you again. – 姚瑞卿 Dec 19 '15 at 10:10
  • No worries. If you want any help regarding angular js, feel free to consult me. I would be happy to help you. – Mohammad Umair Khan Dec 19 '15 at 10:16