1

I am new in angular. I want to access the one controller scope on another first I want to know the simple way and available way to do this, advance thanks

<body ng-app="myApp">
    <div ng-controller="myCtrl">
        First Name: <input type="text" ng-model="fName"><br><br>
        Last Name : <input type="text" ng-model="lName">
        <p ng-bind="fullName()"></p>
    </div>
    <div ng-controller="mySecondCtrl">
        Father Name: <input type="text" ng-model="fathName"><br><br>
        Mother Name:<input type="text" ng-model="mothName">
        <p>
        <strong>Paren name:</strong>
        <span>Mr.{{fathName}},</span>
        <span>Ms.{{mothName}}</span>
        </p>
    </div>

    <script type="text/javascript" src="js/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module("myApp",[]);

        app.controller("myCtrl",myFunction);
        function myFunction($scope){
            $scope.fName = "";
            $scope.lName = "";
            $scope.fullName = function(){
                return $scope.fName +" "+ $scope.lName
            }
        }
        app.controller("mySecondCtrl",mySecondFunction);
        function mySecondFunction($scope){
            //I want to access the fullName() function here
        }
    </script>
</body>
Matthew Walton
  • 9,809
  • 3
  • 27
  • 36
Srikrushna
  • 4,366
  • 2
  • 39
  • 46

1 Answers1

3

You can achieve this purpose by using Angular services for data transfer. You need to set your method in myCtrl, $scope.fullName to a data transfer service.

Now the service holds your method in a variable. Now from your second controller, mySecondCtrl, access the method in the data transfer service.

Now in effect you have called the method in the first controller from the second one. Here is the working example.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>


<body ng-app="myApp">
    <div ng-controller="myCtrl">
        First Name:
        <input type="text" ng-model="fName">
        <br>
        <br> Last Name :
        <input type="text" ng-model="lName">
        <p ng-bind="fullName()"></p>
    </div>
    <div ng-controller="mySecondCtrl">
        Father Name:
        <input type="text" ng-model="fathName">
        <br>
        <br> Mother Name:
        <input type="text" ng-model="mothName">
        <p>
            <strong>Paren name:</strong>
            <span>Mr.{{fathName}},</span>
            <span>Ms.{{mothName}}</span>
        </p>
        <p><button ng-click="getDetails()">Get Details</button></p>
    </div>

    <script type="text/javascript">
        var app = angular.module("myApp", []);

        app.controller("myCtrl", myFunction);

        function myFunction($scope, DataTransfer) {
            $scope.fName = "";
            $scope.lName = "";
            $scope.fullName = function () {
                //console.log('fullname method called');
                return $scope.fName + " " + $scope.lName
            }
            //Setting the method to a data transfer service
            DataTransfer.setUserDetails($scope.fullName);
        }



        app.controller("mySecondCtrl", mySecondFunction);

        function mySecondFunction($scope, DataTransfer) {
            //I want to access the fullName() function here
            $scope.getDetails = function () {
                console.log('Going to call fullname method from second controller');
                //Reading the method in first controller inside the second one
                var functionItem = DataTransfer.getUserDetails();
                var details = functionItem();
                console.log(details);
            }
        }

        app.factory('DataTransfer', function () {

            var data = {};

            return {
                getUserDetails: function () {
                    return data;
                },
                setUserDetails: function (UserDetails) {
                    data = UserDetails;
                }
            };
        });
    </script>
</body>

</html>

Modifications

Here the method is received in a variable in the second controller(functionItem) and that method is called and response data is saved in a variable(details). Now the variable details holds the user details which was taken from first controller.

Nitheesh
  • 19,238
  • 3
  • 22
  • 49