I am new to AngularJS. i am having a following functionality where i am having a FirstName field and LastName Field(in two Different Controllers) and then on the click of a button i want to show FullName(First Name + Last Name) in a third Controller . Please Suggest me how to do. 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">
<h2>Controller 1</h2><br />
<div ng-controller="FirstController">
<input type="text" ng-model="Data.FirstName">
<br>FirstName is : {{Data.FirstName}}</strong>
</div>
<hr>
<h2>Controller 2</h2><br />
<div ng-controller="SecondController">
<input type="text" ng-model="Data.LastName">
<br>LastName is : {{Data.LastName}}</strong>
</div>
<h2>Controller 3</h2><br />
<div ng-controller="ThirdController">
<button ng-click="getName(Data)">Get full Name</button>
FullName is : {{getName(Data)}}
</div>
</div>
</div>
<script>
var myApp = angular.module('myApp', []);
myApp.factory('Data', function () {
return { FirstName: '',LastName:'' };
});
myApp.controller('FirstController', function ($scope, Data) {
$scope.Data = Data;
});
myApp.controller('SecondController', function ($scope, Data) {
$scope.Data = Data;
});
myApp.controller('ThirdController', function ($scope, Data) {
$scope.Data = Data;
$scope.getName = function (Data)
{
$scope.Data = Data;
}
});
</script>
</body>
</html>
You are not injecting your service correctly into your controllers - see this fiddle
Also remove the data param from you getName() call:
<button ng-click="getName()">