0

I guys, in my previous post here I ask how to comunicate between 2 controllers, inside the same file. I have a button where I pass a name inside ng-click with a tab name. When i click in this button the goal is to open a new view in the specified tab inside ng-click.

Each controller have a view instantiated with ng-controller, so they dont share the same view.

At now I have,

dashboardController:

button inside datatable :

return icon = '<center><a class="state-link" data-state-id="' + data.id + '" ng-click="setActiveTab(\'system\')"><i style="color:#0040ff; width:21px; height:21px" title="System threshold exceed" class="fa fa-warning"></i></a></center>';

controllerScope.setActiveTab = function (name) {
    console.log("setActiveTab()");
    console.log("name ",name);
    $rootScope.$broadcast('myCustomEvent', name);
}

dashboardDeviceController:

Here I have the var i want to change

controllerScope.activeTab = {
    consumptions: true,
    network     : false,
    ap          : false,
    modem       : false,
    system      : false,
};

$rootScope.$on('myCustomEvent', function(event, data) {

    console.log("myCustomEvent ", data);
    for (var tabName in controllerScope.activeTab) {
       if (controllerScope.activeTab[tabName] == data) {
          controllerScope.activeTab[tabName] = data;
          break;
       }
    }
});

I have a problem and when I click in the button nothing happen.. so is there a problem with $rootScope.on or $rootScope.broadcast??? I cant figure this out ...

Cátia Matos
  • 820
  • 1
  • 8
  • 26
  • 2
    what is `controllerScope` use `$scope` to broadcast – Sravan Sep 06 '17 at 13:27
  • can you create a [mcve] of the scenario you are trying to solve? looking at the two questions together, it's not immediately obvious how your code is structured and the relationship you actually have between these two controllers. Though @Sravan is probably correct, this could be just a typo. – Claies Sep 06 '17 at 13:29
  • Don't use controllerScope use $scope. `$scope.$broadcast('myCustomEvent', name)` and to receive use `$scope.$on('myCustomEvent', function(event, data){console.log(data)})` you will get name – Prasanna Sep 06 '17 at 13:35
  • I tried and in debug when I click in button it doenst do anyhing.. dont print nothing – Cátia Matos Sep 06 '17 at 13:41
  • `var controllerScope = $scope;` (taken from your comment on an answer) is generally considered a bad practice. aside from it being unnecessary, it makes your code less clear, and could potentially introduce unexpected behaviors. – Claies Sep 06 '17 at 13:44
  • Ok... But I modified and nothing worked anyway ... any ideia? – Cátia Matos Sep 06 '17 at 13:45
  • again, after reading through both this question and your other multiple times, it's still not clear what the relationship between these two controllers actually is. This is *absolutely* a case where a [mcve] with both controllers and the supporting HTML is necessary to make sense of the problem. – Claies Sep 06 '17 at 13:46
  • version of angularjs? – alphapilgrim Sep 06 '17 at 13:47
  • I cant do a example cause it will not work because I need a lot of files and directives.. I have a dahboard view in my dashboardController. My dahboard have a table with data and a button when I click it open a new view of detail info understand? So for this detail info I have a dashboardDeviceController. I need tha part of tabs that's created in this second controller, both in the same file – Cátia Matos Sep 06 '17 at 13:49
  • 1
    it's entirely possible that the controller generating the event is a child scope in the scope hierarchy, and `$scope.$emit` instead of `$scope.$broadcast` might be necessary. – Claies Sep 06 '17 at 13:49
  • 2
    sorry "I can't create an example of the problem" isn't acceptable, that makes the question off topic **immediately**, in my opinion. It isn't necessary to recreate your code *exactly*, but not showing enough for others to try to recreate the problem without throwing random guesses isn't efficient and doesn't work for this site format. I've *almost never* seen a problem that couldn't be recreated in http://plnkr.co, showing the ***essential parts of the problem***. – Claies Sep 06 '17 at 13:51
  • Here's the plunkr I have.. I delete functions that wanst needed for this case to dont become too extensive ... here it is http://plnkr.co/edit/3EHq89phQVRSFxuxX7Ra?p=catalogue. I'm using angular 1.3 – Cátia Matos Sep 06 '17 at 14:04
  • Can you figure the problem?? – Cátia Matos Sep 06 '17 at 14:40
  • don't just use plunker to drop extra code; *the entire point* of creating a plunker is to have a **functional** example of what the problem is, for people to make changes to the code and see what effect it has. If you press play and it doesn't show the problem in the running window, it's not a functional example. – Claies Sep 06 '17 at 20:26

2 Answers2

0

you should use $scope to broadcast and `subscribe,

So, change controllerScope.$broadcast('myCustomEvent', name); to $scope.$broadcast('myCustomEvent', name);

controllerScope.setActiveTab = function (name) {
    console.log("setActiveTab()");
    console.log("name ",name);
    $scope.$broadcast('myCustomEvent', name);
}
Sravan
  • 18,467
  • 3
  • 30
  • 54
0

Have a look at the accepted answer here

You can either broadcast on $rootScope, to have the whole app hear it, or if you don't want to, you need to make sure that the broadcast is meant for the $scope itself and its children. In summary, one controller should be a child of the other.

Dimitrios Matanis
  • 896
  • 1
  • 6
  • 19
  • I've already done that and I click in the button and nothing happens.. in debug i put breakpoints and nothing happens neither printed in console... I tried with rootScope – Cátia Matos Sep 06 '17 at 16:08