service - Share Data in AngularJs -


how share data controller?

i have controller pull data server(file.json) want share other controller

sampleapp.controller('phonelistctrl',  ['$scope', '$http',  function($scope, $http) {     $http.get('app_data/phonelist.json').         success(function(returndatafrmjson){             $scope.phonesscope = returndatafrmjson;         }); }]); 

controller access shared data of first one

sampleapp.controller('addiphonecontroller',  ['$scope', '$http', function($scope, $http) {      $scope.newinput= 'sample text';      $scope.sharedtext= datafromsharedcontroll; }]); 

the html file show data.

{{newinput}} {{sharedtext}} 

well, there various options using: $sessionstorage, localstorage, appconstant, $localstorage , on.

you can share data between controllers using factory , services. giving simple example of using $sessionstorage.

in order use $sessionstorage or $localstorage, need inject ngstorage.

first in index.html, include source:

<script src="https://rawgithub.com/gsklee/ngstorage/master/ngstorage.js"></script>     

then in module definition, inject ngstorage:

var sampleapp = angular.module('your app name', ['ngstorage']);   

and , in controllers:

sampleapp.controller('phonelistctrl',  ['$scope', '$http', '$sessionstorage', function($scope, $http,$sessionstorage) {     $http.get('app_data/phonelist.json').         success(function(returndatafrmjson){             $scope.phonesscope = returndatafrmjson;             $sessionstorage.sharedvalue = returndatafrmjson;//keeping value in session         }); }]);     

controller access shared data of first one

sampleapp.controller('addiphonecontroller',  ['$scope', '$http','$sessionstorage', function($scope, $http,$sessionstorage) {      $scope.newinput= 'sample text';      $scope.sharedtext= $sessionstorage.sharedvalue; }]); 

then in view:

{{newinput}}{{sharedtext}} 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -