Quantcast
Channel: Ionic Forum - Latest posts
Viewing all articles
Browse latest Browse all 228595

Passing data between controller/views

$
0
0

I am having trouble passing data between controllers using a service. What i want to happen is when send data is clicked the data inputted into the text field should be populated in the Results controller. However nothing shows

Home.html:








{{text}}

Send Data



Ctrl2


{{text}}



</ion-view>


HomeController.js:
var app = angular.module('starter', ['ionic'])

    .run(function ($ionicPlatform) {
        $ionicPlatform.ready(function () {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            if (window.cordova && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            }
            if (window.StatusBar) {
                StatusBar.styleDefault();
            }
        });
    })

/*
* Data Service
* Service used to pass data between controllers
*/
app.factory('dataShare', function(){
var service = {};
service.data = false;
service.sendData = function(data){
this.data = data;
$rootScope.$broadcast('data_shared');
};
service.getData = function(){
return this.data;
};
return service;
});

/*
* Stock Update Controller
* Gets user input and then performs calculations to prepare to be displayed
*
*/
app.controller("StockUpdateCtrl", function ($scope, $http, dataShare) {

$scope.text = 'Hey';
     $scope.send = function(){
       dataShare.sendData($scope.text);
     };

});

ResultsController.js:
* Resultse Controller
* Displays the results
*
*/
app.controller("ResultsController",function ($scope, dataShare) {

$scope.text = '';
            $scope.$on('data_shared',function(){
                        var text =  dataShare.getData();
          $scope.text = text;
    });

});


Viewing all articles
Browse latest Browse all 228595

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>