Code Monkey home page Code Monkey logo

angularisms's Introduction

Angularisms

Use bower. The bower.json file already contains dependencies required. All the components are linked from local bower_components.

This repo has code examples with built-in angular (local) support, so I could try these on the train with no WiFi. Somehow plunkr.co had some issues and I thought this was a good way to share stuff.

These are mostly my notes/thoughts based on Ari Lerner's book and my own experiments.

Scopes

Scopes are objects that contain functionality and data to use when rendering the view. It is the single source of truth for all views. You can think of scopes as view models.

What Can Scopes Do?

Scopes have the following basic functions:

  • Provide observers to watch for model changes.
  • Propagate model changes through the application as well as outside the system to other components.
  • Isolate functionality and model properties.
  • Provide an execution environment in which expressions are evaluated.

An AngularJS app builds out functionality of a scope.

We can place data in the $rootScope, the ultimate scope object. But we can also explicitly create a child $scope object using a controller. We can attach a controller object to a DOM element using the ng-controller directive on a DOM element.

The ng-controller directive creates a new $scope object for the DOM element that it's tagged with and nests it in the containing $rootScope. The $scope will be 'made aware' of the model changes.

New Scope Creation

When we create a controller or a directive, Angular creates a new scope with the $injector and passes this new scope for the controller or directive at runtime.

Directives and Scopes

Directives, which are used all throughout our Angular apps, generally do not create their own $scopes, but there are cases when they do. For instance, the ng-controller and ng-repeat directives create their own child scopes and attach them to the DOM element.

Controllers

Controllers in AngularJS exist to augment the view of an AngularJS application.

The controller in AngularJS is a function that adds additional functionality to the scope of the view. We use it to set up an initial state and to add custom behavior to the scope object.

When we create a new controller on a page, Angular passes it a new $scope. This new $scope is where we can set up the initial state of the scope on our controller. Since Angular takes care of handling the controller for us, we only need to write the constructor function. Setting up an initial controller properly looks like this:

    var app = angular.module('app', []); // the app expressed as an Angular module
    app.controller('HelloController', function ($scope) {}); //create a controller that is attached when the ng-controller attribute on an html element says ng-controller='HelloController'

Let us say our html is like this: [https://github.com/kedarmhaswade/angularisms/simple-button-clicks-using-controller-and-directives.html].

One major distinction between AngularJS and other JavaScript frameworks is that the controller is not the appropriate place to do any DOM manipulation or formatting, data manipulation, or state maintenance. It simply holds the $scope/model. It is the glue between the view and the $scope model.

Angular uses scopes to isolate the functionality of the view, controllers, and directives (we’ll cover these later in the book), which makes it very easy to write tests for a specific piece of functionality.

angularisms's People

Contributors

jenkins-li-bot avatar kedarmhaswade avatar

Watchers

 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.