3

Angular Experts,

I am new to Angular Js and trying to update input in one controller with value from the input in another controller. I am not sure whether it is even possible or not?

I have created js fiddle link just to give an idea.

http://jsfiddle.net/MwK4T/

In this example when you enter a value in controller 2's input, and click 'Set Value it does update biding with <li></li> but does't not update input of the controller 1.

Thanks in advance

2
  • I think a search for "communication between Angular controllers" may help you here. It should reveal a few ways, but a fairly standard one would be to have the model provided by a service that is injected into both controllers. Commented Dec 16, 2013 at 8:36
  • Thanks Michal ... I made my solution as below ... Commented Dec 17, 2013 at 20:26

2 Answers 2

18

I made it working using $broadcast. And it is working like a charm.

I did something like this.

App.controller('Ctrl1', function($rootScope, $scope) {
  $rootScope.$broadcast('msgid', msg);
});

App.controller('Ctrl2', function($scope) {
  $scope.$on('msgid', function(event, msg) {
    console.log(msg);
  });
});

Thanks for helping me out.

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

1 Comment

This worked great for me. Wish I could give you more than one +1!
2

Best way would be to use the service, we need to avoid the event as much we can, see this

var app= angular.module('testmodule', []);
app.controller('QuestionsStatusController1',
    ['$rootScope', '$scope', 'myservice',
    function ($rootScope, $scope, myservice) {
       $scope.myservice = myservice;   
    }]);

app.controller('QuestionsStatusController2',
    ['$rootScope', '$scope', 'myservice',
    function ($rootScope, $scope, myservice) {
      $scope.myservice = myservice;
    }]);

app.service('myservice', function() {
      this.xxx = "yyy";
    });

Check the working example

Comments

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.