-1

I have 2 controllers, I want to have next thing: when I click on item in controllerOne it should highlight element with the same ID in controllerTwo. I have highlighting method But how to send event with ID from controllerOne to controllerTwo ??

Serhiy
  • 1,893
  • 9
  • 30
  • 48
  • 2
    Possible duplicate of [Passing data between controllers in Angular JS?](http://stackoverflow.com/questions/20181323/passing-data-between-controllers-in-angular-js) – Manish Sep 12 '16 at 10:39

3 Answers3

1

Keep the shared data in a service that's accessible from both controllers, see https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#defer-controller-logic-to-services

johangu
  • 824
  • 6
  • 12
0

Use $route to pass variables in the URL:

/controller2route/:action/:id

for example:

/details/highlight/2

You can then use ifs and switches to call the appropriate function, for example highlight(2) for the above URL.

https://docs.angularjs.org/api/ngRoute/service/$route

user2807746
  • 121
  • 1
  • 1
  • 8
0

You can you route parameter in routing.

In routing

app.config(function($routeProvider) {
    $routeProvider
    .when("/users/:userId", {
        templateUrl : "main.htm",
        controller:"controllerTwo "
    })
})

and in controllerTwo use $routeParams to get the user id.

app.controller('controllerTwo',function($scope,$routeParams){
  var userId = $routeParams.userId;
})

and the one better solution is to use services. Here is the link https://thinkster.io/a-better-way-to-learn-angularjs/services

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189