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 ??
Asked
Active
Viewed 64 times
3 Answers
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.

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