posted by  on  

If we are using Views in our application and want to pass the data from one controller to another controller just follow given simple steps.

Here we have Single Page Application(SPA) in which if we want to pass data for that this example is given:


<!DOCTYPE html>
<html ng-app="myApp">
    <meta charset="utf-8" />
      Share data from one conroller to another using $routeScope
    <div ng-view=""></div>

<script data-require="angular.js@1.5.x" src="" data-semver="1.5.8"></script>
<script data-require="angular-route@*" data-semver="1.5.8" src=""></script>
<script src="routeApp.js"></script>
<script src="app.js"></script>



Whatever we will write in textbox it will be shown in "View2.html" textbox.

  <label>Text : </label>
  <input type="text" name="fname" value="AbcdXyz" ng-model="fname" />
  <a href="" ng-click="shareData()">Link</a>


  <label>Data :</label>
  <input type="text" name="fname" value="" ng-model="fname" readonly />
  <a href="index.html#/" ng-click="shareData()">Back</a>


This file is for routing to achieve SPA.

var myApp = angular.module('myApp', ['ngRoute', 'yourApp']);
myApp.config(['$routeProvider', function ($routeProvider) {

    templateUrl: 'View1.html',
    controller: 'View1'
    templateUrl: 'View2.html',
    controller: 'View2'
    redirectTo: '/'


Here we are using $rootScope for sharing Data.

var yourApp = angular.module("yourApp",[]);
yourApp.controller("View1", function ($rootScope, $scope, $window) {

$ = $scope.fname;

$scope.shareData = function () {

    $ = $scope.fname;
    $window.location.href = "#/View2";

yourApp.controller("View2", function ($rootScope, $scope, $window) {
  $scope.fname = $;

If you want to view the code Click HERE.

Lateshtclick Technology Blog
Tags AngularJS
posted by Latesht Click Latesht Click


Leave a Reply

Feel free to contribute!