0

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

3
  • Use ng-controller only once(in dashboard.html) and include the header.html inside it. No need to have an ng-controller in header.html. It would be helpful if you post your code. Commented Jan 2, 2015 at 7:49
  • 1
    Controller are not shared. If you using CategoryCtlr at multiple places, a new controller will be created evertime. Show some code. You have to look at scope inheritance and share services to share data. Commented Jan 2, 2015 at 7:50
  • Are these different templates residing in the same HTML document? or are they two different HTML documents? Commented Jan 2, 2015 at 9:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.