concept controller in category angular

appears as: controller, The controller, controllers, controllers, controller, The controllers, A controller
Getting MEAN with Mongo, Express, Angular, and Node.js 2ED

This is an excerpt from Manning's book Getting MEAN with Mongo, Express, Angular, and Node.js 2ED.

3.3.4. Splitting controllers from routes

In a default Express setup, controllers are part of the routes, but you want to separate them out. Controllers should manage the application logic, and routing should map URL requests to controllers.

Looking at the collections you’ve decided on, you decide to split the controllers into Locations and Others. To see how this approach might work from a file-architecture point of view, you can sketch something like figure 4.3. Here, the application includes the routes file, which in turn includes multiple controller files, each named according to the relevant collection.

Figure 4.3. Proposed file architecture for routes and controllers in your application

You have a single route file, as well as one controller file for each logical collection of screens. This setup is designed to help you organize your code in line with how your application is organized. You’ll look at the controllers shortly, but first, you’ll deal with the routes.

Setting up the view and the controller

The first step is creating a new view file and linking it to the controller. In the app_server/views folder, make a copy of the index.pug view, and save it in the same folder as locations-list.pug. It’s best not to call the file homepage or something similar, as at some point, you may change your mind about what should be displayed on the homepage. This way, the name of the view clearly identifies it, and it can be used anywhere without confusion.

The second step is telling the controller for the homepage that you want to use this new view. The controller for the homepage is in the locations.js file in app_server/controllers. Update this file to change the view called by the homelist controller, as shown in the following code snippet (modifications in bold):

These two pieces of content are the first that you’ll move into the controller. The homepage controller currently looks like the following:

Listing 5.2. Data in the controller powering the Details page
location: {
  name: 'Starcups',                                         #1
  address: '125 High Street, Reading, RG6 1PS',             #1
  rating: 3,                                                #1
  facilities: ['Hot drinks', 'Food', 'Premium wifi'],       #1
  coords: {lat: 51.455041, lng: -0.9690884},                #1

    days: 'Monday - Friday',                                #2
    opening: '7:00am',                                      #2
    closing: '7:00pm',                                      #2
    closed: false                                           #2
  },{                                                       #2
    days: 'Saturday',                                       #2
    opening: '8:00am',                                      #2
    closing: '5:00pm',                                      #2
    closed: false                                           #2
  },{                                                       #2
    days: 'Sunday',                                         #2
    closed: true                                            #2
  }],                                                       #2
  reviews: [{                                               #3
    author: 'Simon Holmes',                                 #3
    rating: 5,                                              #3
    timestamp: '16 July 2013',                              #3
    reviewText: 'What a great place.                        #3
  I can\'t say enough good things about it.'              #3
  },{                                                       #3
    author: 'Charlie Chaplin',                              #3
    rating: 3,                                              #3
    timestamp: '16 June 2013',                              #3
    reviewText: 'It was okay. Coffee wasn\'t great,         #3
  but the wifi was fast.'                                 #3
  }]                                                        #3
}
Angular in Action

This is an excerpt from Manning's book Angular in Action.

We’ll start by setting up the template for our component and then we’ll create the controller to manage it. Open the src/app/components/summary/summary.component.html file and replace the contents with what you see in the following listing.

<!-- 1. Calculates the value of two numbers, adds to 30 -->
{{10 + 20}}
<!-- 2. Outputs a string "Just a simple string" -->
{{'Just a simple string'}}
<!-- 3. Binds into an attribute value, to link to profile -->
<a href="/users/{{user.user_id}}">View Profile</a>
<!-- 4. Outputs first and last name -->
{{user.first_name}} {{user.last_name}}
<!-- 5. Calls a method in the controller that should return a string -->
{{getName()}}

Figure 3.9 Event bindings link an event from the template with a method from the controller.

c03-9.png

The first step is to define the form for our invoice, and this is done to ensure that the controller is aware of all of the aspects of the form. This will define a separate model that exists just for the form. When we load the invoice data from the API, we’ll load it into the form, rather than directly bind the form to it like we saw with NgModel.

AngularJS in Action

This is an excerpt from Manning's book AngularJS in Action.

Figure 1.6. Views and controllers
Figure 1.6. Views and controllers

We’ll get to the Model portion in the services section; but for now let’s focus on the View and ViewModel parts of this pattern. The View in the MVVM pattern is the view in AngularJS (naturally), and the controller plays the part of the ViewModel, as you can see in figure 1.7.

Figure 1.7. Model-View-ViewModel

The controller is responsible for providing state for the view to bind to and commands that the view can issue back to the controller to do units of work. This frees up the view from having to maintain state (since it only has to display whatever state the controller is in) and it frees up the view from having to do any work (as the view always defers to the controller).

In this chapter, we’ll get into the most fundamental and important facet in AngularJS. You’ll learn what views and controllers are in AngularJS, and more importantly the relationship that they have with each other. There may be some edge case that we haven’t considered but by our approximation, everything in AngularJS is designed to either support views and controllers, or to extend their functionality.

The goal of this chapter is to build out the storyboard view in Angello, as seen in figure 3.1. This storyboard view will give you plenty of opportunities to learn how a view works in AngularJS and how you can control that view with a controller. You’ll learn how to take an array of JavaScript objects and display them as user stories, and then how to create, update, and delete items in the user story collection by exposing that functionality via the controller.

Before we get into the specific implementation details of how the storyboard view is constructed, we’ll spend a brief moment talking about the relationship between views and controllers, as seen in figure 3.2.

Figure 3.2. View and controller big picture

A view in AngularJS is the HTML after it has been compiled by AngularJS, and is the interface that the user sees. At the core, a controller is a JavaScript object that contains methods and properties. The view contains the declarative markup and the controller possesses code to define imperative behavior. As it stands, the view and controller are completely separated from each other, with nothing to connect the two. This is where scope comes into the picture (see figure 3.3).

Figure 3.3. Scope is the glue

The glue between the view and the controller is scope. The controller is responsible for exposing methods and properties to the view by attaching them to scope. When a method or property is declared on scope, it’s available to interact with the view. For the sake of convenience, most people generally think of the controller and scope as a single entity that behaves essentially like a ViewModel.

If you examine figure 3.4, you can see how this relationship exists with some simple pseudocode. The form in user.html is bound to the user object on $scope in the UserController. When the form changes, $scope.user is immediately updated to reflect the changes. The converse is also true when $scope.user is updated; the form will also update to reflect the state of the object. When the Save button is clicked, the view issues a command to the controller in the form of a $scope.save method call. The controller can also issue commands to services, which you see in the UserModel.save method call. We’ll get into services and models in the following chapter, but for now let’s focus on views and controllers.