npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-decorated

v0.0.7

Published

ES6 decorators for Angular 1.5

Downloads

14

Readme

ng-decorated

Set of ES6 decorators to write Angular 2 style code in AngularJS 1.5.

TODO: @Component HostListener Input Output

NPM

Contents

Installation

npm install ng-decorated --save-dev

Usage

The decorators provided by this package work by adding metadata to your ES6 classes and loading them to Angular .

(1) Import ng-decorated into your project.

(2) Add ngDecorated to your main module's list of dependencies.

import angular from 'angular';
import ngDecorated from 'ng-decorated';

angular.module('myApp', [ngDecorated]);

Decorators

@Component

Defines a component with a template and an isolated scope.

Usage:

import { Component } from 'ng-decorated';

@Component({
  selector: 'myTest',
  template: '<h1>Hello World!</h1>'
})
class MyTestComponent { }

Component Options

See Angular docs for Component for a full list of options.


selector: (Required) This is the selector string for the resulting component.


template: (Optional) Defines a template string for the component.

@RouteConfig

Used with @Component decorator to define the route config for a component.

Usage:

import { Component, RouteConfig } from 'ng-decorated';

@Component({
  selector: 'myTest',
  template: '<h1>Hello World!</h1>'
})
@RouteConfig([
  {path: '/', name: 'Homepage', component: 'homepage', useAsDefault: true},
  {path: '/users/...', name: 'Users', component: 'users'}
])
class MyTestComponent { }

See Angular docs for ComponentRouter docs for more details.

@Directive

Declares an angular directive with decorated class as its controller.

Usage:

import { Directive, @Inject } from 'ng-decorated';

@Directive({
  selector: '[my-attr]'
})
@Inject('$element')
class MyAttrDirective {
  constructor($element) {
    this.element = $element[0];
  }
}

Directive Options

See Angular docs for Directive docs for more details.


selector: (Required) CSS selector to identify the HTML in the template that is associated with the directive.

Examples

  • [my-directive]: Atributte directive (restrict = 'A')
  • .my-directive: Class directive (restrict = 'C')
  • my-directive: Element directive (restrict = 'E') (PS: Consider using @Component)

restrict: (Do not use) This option will be set automatically according to the selector type.


scope: (Do not use) Directives do not have isolated scopes. You need to declare your properties and use the specific decorators to define them:

@Input Angular docs

Declares a data-bound input property.

Angular automatically updates data-bound properties during change detection.

InputMetadata takes an optional parameter that specifies the name used when instantiating a component in the template. When not provided, the name of the decorated property is used.

Usage:

import { Directive, Inject, Input } from 'ng-decorated';

@Directive({ selector: 'my-dir' })
@Inject('$element')
class MyDirective {
  constructor($element) {
    this.el = $element[0];
  }

  /**
   * Markup example:
   * <my-dir bg-color="$ctrl.bgColor"></my-dir>
   */
  @Input()
  set bgColor(color) {
    this.el.style.backgroundColor = color;
  }

  /**
   * Markup example:
   * <my-dir is-bold="true"></my-dir>
   */
  @Input('isBold')
  set fontStyle(bold) {
  	this.el.style.fontWeight = bold ? 'bold' : 'normal';
  }
}

@Output Angular docs

Declares an event-bound output property.

When an output property emits an event, an event handler attached to that event the template is invoked.

OutputMetadata takes an optional parameter that specifies the name used when instantiating a component in the template. When not provided, the name of the decorated property is used.

Usage:

import { Directive, Output, EventEmitter } from 'ng-decorated';

@Directive({ selector: 'interval-dir' })
class MyDirective {
  /**
   * Markup example:
   * <interval-dir every-second="$ctrl.everySecond(param)"></interval-dir>
   */
  @Output()
  everySecond = new EventEmitter();

  /**
   * Markup example:
   * <interval-dir every-five-seconds="$ctrl.everyFiveSecond(param1, param2)"></interval-dir>
   */
  @Output('everyFiveSeconds')
  five5Secs = new EventEmitter();

  constructor() {
    setInterval(() => this.everySecond.emit({ param: 'value' }), 1000);
    setInterval(() => this.five5Secs.emit({ param1: 'value', param2: 'another value' }), 5000);
  }
}

@HostListener Angular docs

Declares a host listener.

Angular will invoke the decorated method when the host element emits the specified event.

Usage:

import { Directive, HostListener } from 'ng-decorated';

@Directive({ selector: '[counting]' })
class CountClicks {
  numberOfClicks = 0;

  /**
   * Markup example:
   * <button counting>Increment</button>
   */
  @HostListener('click', ['$event.target'])
  onClick(btn) {
    console.log('button', btn, 'number of clicks:', this.numberOfClicks++);
  }
}

@Service

Registers a new angular service with the given name.

Usage:

import { Service } from 'ng-decorated';

@Service({
  name: 'myService'
})
class MyService { }

Service Options

name: (Required) This is the name of the registered service.

@Inject

Defines a list services to be injected to a constructor or method.

Usage:

import { Inject } from 'ng-decorated';

@Inject('$q', '$http')
class ExampleService {
  constructor($q, $http) {
    // services are injected to class constructor
  }
}

class AnotherService {
  @Inject('$log')
  static warnToConsole($log) {
    // services can also be injected to a method
  }
}

@Config

Declare an angular config clause with a static method within a class.

Usage:

import { Config } from 'ng-decorated';

class AppConfig {
  @Config()
  static configSomething() {

  }
}

@Run

Declare an angular run clause with a static method within a class.

Usage:

import { Run } from 'ng-decorated';

class AppRun {
  @Run()
  static runSomething() {

  }
}

@Pipe

Declare reusable pipe function. (Called filters in Angular 1.x)

The class must declare the transform method.

Usage:

import { Pipe } from 'ng-decorated';

@Pipe({ name: 'lowercase' })
class Lowercase {
  transform(value) {
    return v.toLowerCase();
  }
}

Pipe Options

name: (Required) This is the name of the registered pipe.

License

The MIT License

Copyright (c)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.