1

When I use a regular Angular controller, I place the controller attribute inside a DOM element, and the controller controls all DOM tree inside this element, e.g.

<div ng-controller="myController">

When I attach a controller via ui-router I do it in my app.js file, via the states config, like this:

.state('report', {
    url: '/site/:site/report',
    templateUrl: 'minderbinder/report/view/report.html',
    controller: 'ReportController'
})

What is the scope of this controller?

1
  • 1
    It would control what is in the templateUrl. Let's just image that, there is a div placeholder and everything inside the templateUrl .html file will be put inside, and the declared controller will be tighted to the div placeholder accordingly. You can see angular-ui-router src to understand how it works. Commented May 31, 2015 at 15:29

1 Answer 1

1

You shouldn't declare your controller manually in your div, unless your intent is to make a child (controller) of the ReportController to control some nested and specific part of your DOM.

As a child, then myController would inherit from ReportController's scope, this latter being the upmost controller of your template.

The benefit of avoiding to declare controller in your HTML is to promote reusability of your HTML template across potential various controllers, using the exact same HTML.

A typical example would be an edit form and an adding form (CRUD) mapped to their own distinct controllers but the same HTML template.

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

2 Comments

ok, so I define the controller via ui-router. What is the scope of this controller? I place {{var}} variables in my code, and I see the values only inside my ui-view elements. I want to place values in my layout, which is wrapping the ui-view.
All vars from this controller are reachable from your template report.html. It ONLY targets the INSIDE of ui-view. Your wrapping view should then declare it's own "parent" state, with its own controller defined. Let's say for example a state named main (targeting your layout) and another state named main.report (targeting the nested ui-view within your layout).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.