abdulbari
abdulbari

Reputation: 6242

Getting two Scopes of same controller in Angular.js

I just started to work with Angular, it's pretty nice to work with it, I just facing an issue of controllers's scope.

I have two files

1- header.html

2-dashboard.html

I included header.html in dashboard.html

I am performing two tasks with these files. I have to use ng-click="viewAll()" which comes under CategoryCtlr controller in header.html, and after performing event I have to show the result in dashboard.html's element which is here

<div class="row articleAll"></div>

if I am using this element in header.html it's working fine but, not in dashboard.html

I inject ng-controller="CategoryCtlr" in both files.

Can anybody help me to handle this task,

It will be grateful for me.

Thank You

Upvotes: 0

Views: 514

Answers (1)

Suneet Bansal
Suneet Bansal

Reputation: 2702

Try to create two controllers one is for header.html and another is for dashboard.html. Lets say A_Cntrl is for dashboard and B_Cntrl is for header.html

Now as you are injecting header.html inside dashboard.html, A_Cntrl will be the parent controller for B_Cntrl

Use $emit() and $on concept of event notification from child to parent.

Now in viewAll() method,to notify the parent's controller, you put the following code:
$scope.$emit("Notify_To_Parent", $scope.flag(lets assume, change it as per your need);

In the parent controller, put the following code:
$scope.$on("Notify_To_Parent", function(event, state){
    // state is the value which is coming from child's controller
});

Try this, it will help you.

Upvotes: 1

Related Questions