1

I am using factory method in AngularJS to show the combination of First Name and Last Name (first two controllers) as Full Name(in third Controller), but i am not getting the desired output. here is my code:

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>

<body>
   <div ng-app="myApp">

		<div ng-controller="FirstCtrl">
			<input type="text" ng-model="Data.FirstName">
			<br>FirstName is : <strong>{{Data.FirstName}}</strong>
		</div>
		<hr>
		<div ng-controller="SecondCtrl">
            <input type="text" ng-model="Data.LastName">
			 <br>LastName is : <strong>{{Data.LastName}}</strong>
		</div>
       <div ng-controller="ThirdCtrl">
			FullName is : {{Data.FirstName + " " + Data.LastName}}
		</div>

	</div>
	</div>

    <script>
        var myApp = angular.module('myApp', []);


        myApp.factory('Data', function () {
            return { FirstName: '',LastName:'' };
        });

        myApp.controller('FirstCtrl', function ($scope, Data) {
            $scope.Data = Data;
        });

        myApp.controller('SecondCtrl', function ($scope, Data) {
            $scope.Data = Data;
        });
        myApp.controller('ThirdCrl', function ($scope, Data) {
            $scope.Data = Data;
        }); 
    </script>
</body>
</html>

Can you please Help?

1
  • 1
    ThirdCtrl is mispelled Commented Feb 10, 2015 at 6:46

2 Answers 2

1

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>

<body>
   <div ng-app="myApp">

		<div ng-controller="FirstCtrl">
			<input type="text" ng-model="Data.FirstName">
			<br>FirstName is : <strong>{{Data.FirstName}}</strong>
		</div>
		<hr>
		<div ng-controller="SecondCtrl">
            <input type="text" ng-model="Data.LastName">
			 <br>LastName is : <strong>{{Data.LastName}}</strong>
		</div>
       <div ng-controller="ThirdCtrl">
			FullName is : {{Data.FirstName + " " + Data.LastName}}
		</div>

	</div>
	</div>

    <script>
        var myApp = angular.module('myApp', []);


        myApp.factory('Data', function () {
            return { FirstName: '',LastName:'' };
        });

        myApp.controller('FirstCtrl', function ($scope, Data) {
            $scope.Data = Data;
        });

        myApp.controller('SecondCtrl', function ($scope, Data) {
            $scope.Data = Data;
        });
        myApp.controller('ThirdCtrl', function ($scope, Data) {
            $scope.Data = Data;
        }); 
    </script>
</body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

0

It seems you have spelled wrongly the third controller name.

1 Comment

when you are getting errors such as 'Error: [ng:areq] ', its because the function you declared is undefined. In the above case, you declared the controller named as 'ThirdCtrl', but you haven't defined the function (because of spell mistake).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.