Git Product home page Git Product logo

angular-review's Introduction

Angular Review

Let's build an Angular app from scratch!

Outline of Steps

  1. 'Bootstrapping' our Angular app 0. Create application files index.html and script.js 0. Bring in Angular codebase from a CDN 0. Define a module on the angular object 0. Link this module using the directive ng-app
  2. Data & Template-binding 0. Create an array of hard-coded data in app.js 0. Define a controller on the angular object 0. Define the controller's function (callback function) 0. Define a property on the ControllerFunction
    • e.g. this.todos = todosArray 0. Bind the controller to a DOM element in index.html using the directive ng-controller 0. Use the directive ng-repeat to iterate over the collection in vm.todos
  3. Replace ng-controller with state from ui-router 0. Remove ng-controller directive 0. Bring in ui.router from a CDN 0. Inject ui.router in the modules array of dependencies 0. Define a config on the module. This will both inject $stateProvider and reference the RouterFunction 0. Define your RouterFunction, injecting $stateProvider 0. Add one state onto $stateProvider 0. Add a new template todos-index.html 0. Add ui-view to <main>

Step 1: Setup

 $ mkdir angular_practice
 $ touch index.html app.js

Let's add in some HTML boilerplate.

in index.html:

  <!DOCTYPE html>
  <html>
    <head>
      <meta charset="utf-8">
      <title></title>
      <!-- Bring in Angular CDN here -->
      <script src="app.js"></script>
    </head>
    <body>

    </body>
  </html>

Next, we'll add in the Angular codebase from a CDN, and then add a module to the angular object. If it seems odd that there is an angular object. Think back to jQuery/$. It's the same idea of a library/framework being encapsulated in a single, imported object.

in app.js:

angular
  .module("todosAgain",[])

in index.html:

<body ng-app="todosAgain">
  {{3/0}}
</body>

Step 2: Adding the Controller

Since Controllers distribute data and an application's business logic, let's add in some hard-coded data.

in app.js

let todosData = [
  {
    task: "Grind out Angular apps all day so I can stack $stacks",
    completed: true
  },
  {
    task: "Order 500lbs of Cheetos with this Angular-dev paycheck",
    completed: false
  },
  {
    task: "Monetize my meme-stream",
    completed: true
  },
  {
    task: "Create Machine Learning platform optimized for dank memes",
    completed: false
  }
]

Next, we will add a controller onto the angular object.

in app.js

angular
  .module("todosAgain",[])
  .controller("TodosController", TodosControllerFunction)

We'll also have to define the TodosControllerFunction.

function TodosControllerFunction(){
  this.todos = todosData;
}

And finally let's bind the controller to a template (index.html). Template-binding is simply attaching a controller to a template. We will do this using ng-controller. Let's add in a <main></main> and bind the controller to it.

in index.html

<body ng-app="todosAgain">
  <!-- we're creating a new instance of our controller and saving it to a variable vm -->
  <main ng-controller="TodosController as vm">

  </main>
</body>

inside the main tag :

<div ng-repeat="todo in vm.todos">
  {{todo.task}}
</div>

Step 3: Bringing in UI Router

First, we'll grab UI Router from a CDN and bring it into our application.

Next, we'll add a config to the angular object. A state in Angular is basically a route: it's an umbrella term for a URL, the view associated with it, and any controllers used in that view.

angular
  .module("todosAgain",["ui.router"])
  .config([
    "$stateProvider",
     RouterFunction
   ])
  .controller("TodosController",  ["$stateParams", TodosControllerFunction])

  function TodosControllerFunction($state, $stateParams){
  this.todos = todosData;
}

Next, we'll define RouterFunction:

function RouterFunction($stateProvider){
  $stateProvider
    .state("todosIndex", {
      url: "/",
      templateUrl: "todos-index.html",
      controller: "TodosController",
      controllerAs: "vm"
    })
}

What is a state doing here? It's binding a controller to a template, and making that binding available at a route, in this case /.

We haven't yet made the new template todos-index.html so let's do that.

Now, we'll cut this HTML from index.html and paste it to todos-index.html.

<div ng-repeat="todo in vm.todos">
  {{todo.task}}
</div>

Then, we'll remove ng-controller and replace it with ui-view:

<main ui-view>

</main>

Run your server by typing in hs. Open your browser to http://localhost:8080/#/ and voila!

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.