K
K
k-22016-05-22 12:28:18
Angular
k-2, 2016-05-22 12:28:18

How to pass events (data) to another controller?

How to make controller2 work with data in controller1. And accordingly changed the data in controller1.
There can be many controllers2(3,4..), so it will not be possible to write a dependency on them all in controller1.

<html ng-app="myApp">
<body>

<div ng-controller="phoneController">
<p>Название: {{phone.name}}</p>
</div>

<div ng-controller="phoneController2">
<input type="text" ng-model="phone.name" / >
<p>Название: {{phone.name}}</p>
</div>

<script src="js/lib/angular.min.js"></script>
<script type="text/javascript">
var myApp=angular.module('myApp', []);

myApp.controller('phoneController', function($scope) {
 
  $scope.phone = {
        name: 'Nokia Lumia 630'
    }
});

myApp.controller('phoneController2', function($scope) {
 
  $scope.phone = {
        name: 'Nokia Lumia 630'
    }
});

</script>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-05-22
@K-2

insert between them the service that you are already asking to change the state. And in the controller, subscribe to the state change.
In general, we are waiting for the new documentation to be merged: https://github.com/angular/angular.js/pull/13834 https://github.com/angular/angular.js/pull/14416
Self-contained controllers like $scope are no longer needed .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question