i am new in angular. so trying to know how to share data between two controller and search google. i visited few pages and found most of the time people use factory to share data. i just like to know can't we do it by service instead of factory ?
1st example
- <div ng-controller="FirstCtrl">
- <input type="text" ng-model="data.firstName">
- <br>Input is : <strong>{{data.firstName}}</strong>
- </div>
- <hr>
- <div ng-controller="SecondCtrl">
- Input should also be here: {{data.firstName}}
- </div>
-
- myApp.factory('MyService', function(){
- return {
- data: {
- firstName: '',
- lastName: ''
- },
- update: function(first, last) {
-
- this.data.firstName = first;
- this.data.lastName = last;
- }
- };
- });
-
-
- myApp.controller('SecondCtrl', function($scope, MyService){
- $scope.data = MyService.data;
-
- $scope.updateData = function(first, last) {
- MyService.update(first, last);
- }
- });
2nd example
- var myApp = angular.module('myApp', []);
-
- myApp.factory('Data', function(){
-
- var service = {
- FirstName: '',
- setFirstName: function(name) {
-
-
-
- angular.copy(name, service.FirstName);
- }
- };
- return service;
- });
-
-
-
- myApp.controller('FirstCtrl', function( $scope, Data ){
-
- });
-
-
- myApp.controller('SecondCtrl', function( $scope, Data ){
- $scope.FirstName = Data.FirstName;
- });
examples are taken from this url https://stackoverflow.com/questions/21919962/share-data-between-angularjs-controllers
please guide me.