0

I want to pass my first view textbox value to another view textbox. and both of that view are using a different controller.

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title></title>
</head>
<body>
    <div ng-view="">

    </div>
</body>
</html>


<script src="../Scripts/angular.min.js"></script>
<script src="../Scripts/angular-route.min.js"></script>
<script src="../Scripts/routeApp.js"></script>
<script src="../Scripts/Controller.js"></script>

This is my main page on which View1 and view2 HTML file will load.

"View1"

<div>
    <input type="text" name="fname" value="" ng-model="fname" />
    <a href="#/View2" >Link</a>
</div>

"View2"

<div>
    <input type="text" name="fname" value="" ng-model="fname" />
</div>

"routeApp.js"

var myApp = angular.module('myApp', ['ngRoute', 'yourApp']);
debugger
myApp.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
    .when('/',
    {
        templateUrl: 'View1.html',
        controller: 'View1'
    }).
    when('/View2',
    {
        templateUrl: 'View2.html',
        controller: 'View2'
    }).
    otherwise(
    {
        redirectTo: '/'
    });
}]);

"Controller.js"

var yourApp = angular.module("yourApp",[]);

yourApp.controller("View1", function ($rootScope, $scope) {
    debugger;
    $rootScope.abc = $scope.fname;
});

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

My call is coming to controller but I know I did a mistake beacause of that i am not getting my value on View2.html page.

Ankit Modi
  • 25
  • 1
  • 9

2 Answers2

0

At the time of assigning $rootScope.abc = $scope.fname, the value of $scope.fname is undefined. You need to save the value before you change your state. You can achieve it by using $destroy event:

$scope.$on("$destroy", function(){
   $rootScope.abc = $scope.fname;
});

Click here to view in Plunker

MrNobody007
  • 1,827
  • 11
  • 32
0

Changes in "Controlle.js" and now it's working fine for me...

var yourApp = angular.module("yourApp",[]);

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

    $scope.shareData = function () {
        debugger;
        $rootScope.abc = $scope.fname;
        $window.location.href = "#/View2";
    }
});

yourApp.controller("View2", function ($rootScope, $scope, $window) {
    debugger;
    $scope.fname = $rootScope.abc;
});
Ankit Modi
  • 25
  • 1
  • 9