0

Making simple program to share data as input in one to be updated in other through custom service only. I am writing bit of code I can, but need help to get it working for both controllers. Can someone help?

SNIPPET:

<html>

<head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 
</head> 

<body ng-app="myApp"> 

<div class="div1" ng-controller="myCtrl1"> 
    <input type="text" ng-model="name"> <span>{{name}}</span>
</div> 

<div class="div2" ng-controller="myCtrl2"> 
    <span>{{name}}</span> 
</div> 

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

//controllers declaration
app.controller('myCtrl1',function($scope, dummyService){
    $scope.name = "Peter";
});

app.controller('myCtrl2',function($scope, dummyService){
    $scope.name = "Roger";
});

//service declaration
app.service('dummyService',function(){
    this.name = "Martin";
});

</script> 

</body> 

</html> 

REQUIREMENT:

As soon as I start typing text in input box. It must start updating the same immediately in both the spans (viz in div1 and in div2).

Deadpool
  • 7,811
  • 9
  • 44
  • 88

3 Answers3

1

1st thing you haven't binded service variable with $scope variable, but that is not going to fix it.

The problem is, you are using primitive type variable where you assigned new value to variable each time it changes the reference of that object. You can solve this problem easily by changing it to reference type object. There by binding object references you ensure that whenever object properties changes it will change the copy of underlying object which it points to. Likewise here we pointed our service model variable with scope's model object they will ensure to keep in sync.

Markup

<body ng-app="myApp">

  <div class="div1" ng-controller="myCtrl1">
    <input type="text" ng-model="model.name"> <span>{{name}}</span>
  </div>

  <div class="div2" ng-controller="myCtrl2">
    <span>{{model.name}}</span>
  </div>

</body>

Code

app.controller('myCtrl1', function($scope, dummyService) {
  $scope.model = dummyService.model;
});

app.controller('myCtrl2', function($scope, dummyService) {
  $scope.model = dummyService.model;
});

//service declaration
app.service('dummyService', function() {
  this.model = {
    name: "Martin"
  };
});

Demo


You can achieve the same thing with primitive type as well, with what you had in your question. But for the same you have to write getter & setter to set variable value. You could change service to below

app.service('dummyService', function() {
  var self = this;
  var name = "Martin"; //private variable

  self.setName = function(name) {
    name = name;
  }
  self.getName = function() {
    return name;
  }
});

Plunkr Here

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
1

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

//controllers declaration
app.controller('myCtrl1',function($scope, dummyService, $rootScope){
    $scope.name = "Peter";
    $scope.change = function() {
      $rootScope.$emit('changeName',{name:$scope.name});
    }
});

app.controller('myCtrl2',function($scope, dummyService, $rootScope){
    $scope.name = "Roger";
    $rootScope.$on('changeName', function(event,data) {
      $scope.name = data.name;
    })
});

//service declaration
app.service('dummyService',function(){
    this.name = "Martin";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp"> 

<div class="div1" ng-controller="myCtrl1"> 
    <input type="text" ng-model="name" ng-change="change()"> <span>{{name}}</span>
</div> 

<div class="div2" ng-controller="myCtrl2"> 
    <span>{{name}}</span> 
</div> 
 
</body>
ram1993
  • 979
  • 1
  • 9
  • 13
1

You can achieve this through method reference. Considering, you need to initialize both name variables with different values. Otherwise, you can simply use service variable.($scope.name = dummyService.name)

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

//controllers declaration
app.controller('myCtrl1',function($scope, dummyService, $rootScope){
    $scope.name = "Peter";
    $scope.change = function() {
      dummyService.setName($scope.name);
    }
    
    
});

app.controller('myCtrl2',function($scope, dummyService, $rootScope){
    $scope.name = "Roger";
    $scope.change = function(name) {
      $scope.name =  name;
    }
    $scope.init = function() {
      dummyService.set($scope.change);
    }
    $scope.init();
});

//service declaration
app.service('dummyService',function(){
    this.name = "Martin";
    this.set = function(method) {
      this.setName = method;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp"> 

<div class="div1" ng-controller="myCtrl1"> 
    <input type="text" ng-model="name" ng-change="change()"> <span>{{name}}</span>
</div> 

<div class="div2" ng-controller="myCtrl2"> 
    <span>{{name}}</span> 
</div> 
 
</body>
ram1993
  • 979
  • 1
  • 9
  • 13