Why and When to Use Controllers and Scopes

Controllers act as the link between the domain model and the view; they provide data and services to the view and define the business logic required to translate user actions into changes in the model.

You can’t build an AngularJS application without a controller; it is one of the fundamental building blocks of the MVC model, as described in Chapter 3. You can, however, decide how many controllers your application has, how they are organized, and how they expose data and functionality to the views they support.

Controllers provide data and logic to views through scopes, which underpin the data binding techniques I demonstrated in earlier chapters and which are a signature feature of AngularJS development. Knowing how scopes work will give you a greater understanding of how AngularJS is designed, even though there are quirks and gotchas that lie on the path to comprehension. Table 13-2 summarizes why and when controllers are used in AngularJS.

Source: Freeman Adam (2014), Pro AngularJS (Expert’s Voice in Web Development), Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *