Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...