Code Monkey home page Code Monkey logo

angular-component-method-readme's Introduction

Bonus: Using the component() method

Overview

Angular 1.5 brought the awesome .component directive. Don't worry, the learning curve isn't massive - it's basically a simplified directive!

Objectives

  • Describe component() and directive() differences

component()

We use components every day - be it a dropdown box, a login form or a navigation bar. Components are awesome - much like directives, they're reusable, isolated pieces of functionality that we can use again and again. Other frameworks provide similar abilities - in fact the whole of React is based off of components - nothing else!

Instead of passing through a function to our component method (like what we do with the directive method), we pass through an object instead. This is really similar to the object we return in our directives.

For example:

function Counter() {
	return {
		template: [
			'<div ng-click="counter.increment()" class="counter">',
				'<h3>Counter</h3>',
				'<div>Click anywhere to increment the counter!</div>',
				'<div class="counter__count">Current count: {{ counter.count }}</div>',
			'</div>'
		].join(''),
		controller: function ($scope) {
			this.count = 0;

			this.increment = function () {
				this.count++;
			};
		},
		controllerAs: 'counter'
	}
}

angular
	.module('app')
	.directive('counter', Counter);

Would turn into this:

var Counter =  {
	template: [
	    '<div ng-click="counter.increment()" class="counter">',
	        '<h3>Counter</h3>',
	        '<div>Click anywhere to increment the counter!</div>',
	        '<div class="counter__count">Current count: {{ counter.count }}</div>',
	    '</div>'
	].join(''),
	controller: function ($scope) {
	    this.count = 0;

	    this.increment = function () {
	        this.count++;
	    };
	},
	controllerAs: 'counter'
};

angular
	.module('app')
	.component('counter', Counter);

Pretty much the same thing! However, there are a few restrictions to using the .component() method:

  • Every item is an element - we can't create attributes
  • We no longer use the scope property. Everything is bound to the controller
  • We no longer use bindToController - this is simply named bindings instead
  • When we use require, we have to pass through an object instead of a string, with the property being anything that we want and the string being what we'd normally use
    • For example, instead of { require: '^parentController' }, we use { require: { parent: '^parentController' } }
  • We cannot use a link or compile function

Then why even use them? Well, directives are old and a bit verbose for what we may need 99% of the time. Components are a new, updated method that has matched the way the frontend has changed. Think of them as a simple way to write a directive, and if you need to go complicated, use a directive instead.

angular-component-method-readme's People

Contributors

ipc103 avatar toddmotto avatar

Watchers

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

angular-component-method-readme's Issues

Not sure this is a good example

As far as I can tell, there is only a couple of small changes in these two large code blocks. Then the text below goes on to talk about scope, binding, and require. I think this example would be better if it included scope, binding, and require.

@ipc103 @PeterBell

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.