Code Monkey home page Code Monkey logo

angular-what-is-a-controller-readme's Introduction

What is a Controller?

Overview

We've talked about controllers before, and now is the time to go into them. Controllers make up a lot of the code we write in Angular so it's important that we learn how to use them correctly and efficiently!

Objectives

  • Describe the role of a Controller
  • Create a Controller
  • Assign the Controller to the module
  • Create an element for Controller scope

Controllers

You might have noticed that we've mentioned the concept of Controllers quite a lot. These are the backbone for your Angular applications, providing the link between the view and the model. Controllers control our data - they handle everything from the user's input to notifications from the backend telling us that data has been updated. Think of them as the brain - co-ordinating everything.

Controllers differ slightly depending on your architecture. If we're using MVC our controller does a lot more than if we're using MVVM. The nicknames for the controller in these situations are fat and thin controllers.

Business/Presentational Logic

We'll be using the terms "business" and "presentational" logic quite a lot, so it's important that we understand what these are. Both of these can happen inside of our controllers, depending on our architectural approach.

Business logic is all the logic we have to do in order to manipulate our model. For instance, retrieving data from an API endpoint and updating it. If we go back to our coffee shop example, the business logic would be the barista making our coffee. The end user doesn't need to have any idea how their coffee is made - all they care about is that it's made. Everything else is behind the scenes.

Presentational logic is when we adapt our model to be displayed correctly in the view. This would be the barista adding our name to the cup as they hand us the drink. They don't care what the coffee is - only that it's there and that they've written your name on the lid.

MVC: Fat Controller

A fat controller deals with a lot. The controller will fetch all of the model data we need and do all of the business logic/presentational logic.

MVVM: Thin Controllers

A thin controller is quite lazy. It hands off all of the business logic to external helpers, such as a service. The controller then handles all of the presentational logic.

Let's get creative

If you recall our first ever lab, we actually created a controller:

function MainController($scope) {
  $scope.name = 'PUT YOUR NAME HERE!';
}

angular
  .module('app')
  .controller('MainController', MainController);

Our controllers are just JavaScript functions. We then use $scope (we're covering everything you'll need to know about $scope in the next lesson). In short, $scope allows us to put information into our views. This is why our first lab has "Hello, PUT YOUR NAME HERE!" when you load the page!

We also assigned the controller to our module named app.

We then referenced and initiated that controller in our HTML:

<div ng-app="app">
  <div ng-controller="MainController">
    Hello, {{ name }}!
  </div>
</div>

It's important that we initiated our controller inside our initiated module app because our controller is assigned to the module app. If we tried to use it in a different module, we'd get an error.

View What is a Controller? on Learn.co and start learning to code for free.

angular-what-is-a-controller-readme's People

Contributors

gj avatar ipc103 avatar toddmotto avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.