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).

8
  • Is no one capable of answering the question? Alas ... ;( Commented Sep 27, 2016 at 7:48
  • Try this: stackoverflow.com/questions/21919962/… Commented Sep 27, 2016 at 7:51
  • 1
    @Peterson I'm on the way :) Commented Sep 27, 2016 at 7:52
  • 1
    So the most active person at SO .. speaks up! ;) Commented Sep 27, 2016 at 7:53
  • @MichałStyś: Already, saw that before posting! Commented Sep 27, 2016 at 7:54

3 Answers 3

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>

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

2 Comments

Polluting $rootScope would be a bad idea
This works ... GG good! Though I think Pankaj is right, but pankaj your code above not working out in plunker!
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>

Comments

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

5 Comments

Pankaj in Plunker the value is not getting updated in second div as is filled and seen changed in first div! Pl check ...
Both of your fiddles are not working. Pl, have a view! (Try change text in input)
@Peterson plunkr seems down. isitdownrightnow.com/plnkr.co.html
P - Thanks Buddy!
@Peterson now they are fixed, Thanks :)

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.