Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce6b364

Browse files
committed
docs(core): add docs to Property and Event
1 parent ef61b81 commit ce6b364

File tree

2 files changed

+143
-119
lines changed

2 files changed

+143
-119
lines changed

modules/angular2/src/core/metadata.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -454,17 +454,7 @@ export interface PipeFactory {
454454
/**
455455
* {@link PropertyMetadata} factory for creating decorators.
456456
*
457-
* ## Example as TypeScript Decorator
458-
*
459-
* ```
460-
* @Directive({
461-
* selector: 'sample-dir'
462-
* })
463-
* class SampleDir {
464-
* @Property() property; // Same as @Property('property') property;
465-
* @Property("el-property") dirProperty;
466-
* }
467-
* ```
457+
* See {@link PropertyMetadata}.
468458
*/
469459
export interface PropertyFactory {
470460
(bindingPropertyName?: string): any;
@@ -474,17 +464,7 @@ export interface PropertyFactory {
474464
/**
475465
* {@link EventMetadata} factory for creating decorators.
476466
*
477-
* ## Example as TypeScript Decorator
478-
*
479-
* ```
480-
* @Directive({
481-
* selector: 'sample-dir'
482-
* })
483-
* class SampleDir {
484-
* @Event() event = new EventEmitter(); // Same as @Event('event') event = new EventEmitter();
485-
* @Event("el-event") dirEvent = new EventEmitter();
486-
* }
487-
* ```
467+
* See {@link EventMetadata}.
488468
*/
489469
export interface EventFactory {
490470
(bindingPropertyName?: string): any;
@@ -588,11 +568,15 @@ export var Pipe: PipeFactory = <PipeFactory>makeDecorator(PipeMetadata);
588568

589569
/**
590570
* {@link PropertyMetadata} factory function.
571+
*
572+
* See {@link PropertyMetadata}.
591573
*/
592574
export var Property: PropertyFactory = makePropDecorator(PropertyMetadata);
593575

594576
/**
595577
* {@link EventMetadata} factory function.
578+
*
579+
* See {@link EventMetadata}.
596580
*/
597581
export var Event: EventFactory = makePropDecorator(EventMetadata);
598582

modules/angular2/src/core/metadata/directives.ts

Lines changed: 137 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -417,116 +417,98 @@ export class DirectiveMetadata extends InjectableMetadata {
417417
selector: string;
418418

419419
/**
420-
* Enumerates the set of properties that accept data binding for a directive.
420+
* Enumerates the set of data-bound properties for a directive
421+
*
422+
* Angular automatically updates data-bound properties during change detection.
421423
*
422424
* The `properties` property defines a set of `directiveProperty` to `bindingProperty`
423425
* configuration:
424426
*
425427
* - `directiveProperty` specifies the component property where the value is written.
426428
* - `bindingProperty` specifies the DOM property where the value is read from.
427429
*
428-
* You can include a {@link PipeMetadata} when specifying a `bindingProperty` to allow for data
429-
* transformation and structural change detection of the value. These pipes will be evaluated in
430-
* the context of this component.
431-
*
432-
* ## Syntax
433-
*
434-
* There is no need to specify both `directiveProperty` and `bindingProperty` when they both have
435-
* the same value.
436-
*
437-
* ```
438-
* @Directive({
439-
* properties: [
440-
* 'propertyName', // shorthand notation for 'propertyName: propertyName'
441-
* 'directiveProperty1: bindingProperty1',
442-
* 'directiveProperty2: bindingProperty2 | pipe1 | ...',
443-
* ...
444-
* ]
445-
* }
446-
* ```
447-
*
430+
* When `bindingProperty` is not provided, it is assumed to be equal to `directiveProperty`.
448431
*
449-
* ## Basic Property Binding
432+
* ### Example ([live demo](http://plnkr.co/edit/ivhfXY?p=preview))
450433
*
451-
* We can easily build a simple `Tooltip` directive that exposes a `tooltip` property, which can
452-
* be used in templates with standard Angular syntax. For example:
434+
* The following example creates a component with two data-bound properties.
453435
*
454-
* ```
455-
* @Directive({
456-
* selector: '[tooltip]',
457-
* properties: [
458-
* 'text: tooltip'
459-
* ]
436+
* ```typescript
437+
* @Component({
438+
* selector: 'bank-account',
439+
* properties: ['bankName', 'id: account-id']
460440
* })
461-
* class Tooltip {
462-
* set text(value: string) {
463-
* // This will get called every time with the new value when the 'tooltip' property changes
464-
* }
465-
* }
466-
* ```
467-
*
468-
* We can then bind to the `tooltip' property as either an expression (`someExpression`) or as a
469-
* string literal, as shown in the HTML template below:
470-
*
471-
* ```html
472-
* <div [tooltip]="someExpression">...</div>
473-
* <div tooltip="Some Text">...</div>
474-
* ```
475-
*
476-
* Whenever the `someExpression` expression changes, the `properties` declaration instructs
477-
* Angular to update the `Tooltip`'s `text` property.
441+
* @View({
442+
* template: `
443+
* Bank Name: {{bankName}}
444+
* Account Id: {{id}}
445+
* `
446+
* })
447+
* class BankAccount {
448+
* bankName: string;
449+
* id: string;
478450
*
479-
* ### Bindings With Pipes
451+
* // this property is not bound, and won't be automatically updated by Angular
452+
* normalizedBankName: string;
453+
* }
480454
*
481-
* You can use pipes in bindings, as follows:
455+
* @Component({selector: 'app'})
456+
* @View({
457+
* template: `
458+
* <bank-account bank-name="RBC" account-id="4747"></bank-account>
459+
* `,
460+
* directives: [BankAccount]
461+
* })
462+
* class App {}
482463
*
483-
* ```html
484-
* <div [class-set]="someExpression | somePipe">
464+
* bootstrap(App);
485465
* ```
486466
*
487467
*/
488468
properties: string[];
489469

490470
/**
491-
* Enumerates the set of emitted events.
471+
* Enumerates the set of event-bound properties.
492472
*
493-
* ## Syntax
494-
*
495-
* ```
496-
* @Component({
497-
* events: ['statusChange']
498-
* })
499-
* class TaskComponent {
500-
* statusChange: EventEmitter;
473+
* When an event-bound property emits an event, an event handler attached to that event
474+
* the template is invoked.
501475
*
502-
* constructor() {
503-
* this.statusChange = new EventEmitter();
504-
* }
476+
* The `events` property defines a set of `directiveProperty` to `bindingProperty`
477+
* configuration:
505478
*
506-
* onComplete() {
507-
* this.statusChange.next('completed');
508-
* }
509-
* }
510-
* ```
479+
* - `directiveProperty` specifies the component property that emits events.
480+
* - `bindingProperty` specifies the DOM property the event handler is attached to.
511481
*
512-
* Use `propertyName: eventName` when the event emitter property name is different from the name
513-
* of the emitted event:
482+
* ### Example ([live demo](http://plnkr.co/edit/d5CNq7?p=preview))
514483
*
515-
* ```
516-
* @Component({
517-
* events: ['status: statusChange']
484+
* ```typescript
485+
* @Directive({
486+
* selector: 'interval-dir',
487+
* events: ['everySecond', 'five5Secs: everyFiveSeconds']
518488
* })
519-
* class TaskComponent {
520-
* status: EventEmitter;
489+
* class IntervalDir {
490+
* everySecond = new EventEmitter();
491+
* five5Secs = new EventEmitter();
521492
*
522493
* constructor() {
523-
* this.status = new EventEmitter();
494+
* setInterval(() => this.everySecond.next("event"), 1000);
495+
* setInterval(() => this.five5Secs.next("event"), 5000);
524496
* }
497+
* }
525498
*
526-
* onComplete() {
527-
* this.status.next('completed');
528-
* }
499+
* @Component({selector: 'app'})
500+
* @View({
501+
* template: `
502+
* <interval-dir (every-second)="everySecond()" (every-five-seconds)="everyFiveSeconds()">
503+
* </interval-dir>
504+
* `,
505+
* directives: [IntervalDir]
506+
* })
507+
* class App {
508+
* everySecond() { console.log('second'); }
509+
* everyFiveSeconds() { console.log('five seconds'); }
529510
* }
511+
* bootstrap(App);
530512
* ```
531513
*
532514
*/
@@ -940,38 +922,94 @@ export class PipeMetadata extends InjectableMetadata {
940922
}
941923

942924
/**
943-
* Declare a bound field.
925+
* Declares a data-bound property.
944926
*
945-
* ## Example
927+
* Angular automatically updates data-bound properties during change detection.
946928
*
947-
* ```
948-
* @Directive({
949-
* selector: 'sample-dir'
929+
* `PropertyMetadata` takes an optional parameters that specifies that name
930+
* used when instantiating a component in the template. When not provided,
931+
* the class property name is used.
932+
*
933+
* ### Example
934+
*
935+
* The following example creates a component with two data-bound properties.
936+
*
937+
* ```typescript
938+
* @Component({selector: 'bank-account'})
939+
* @View({
940+
* template: `
941+
* Bank Name: {{bankName}}
942+
* Account Id: {{id}}
943+
* `
950944
* })
951-
* class SampleDir {
952-
* @Property() property; // Same as @Property('property') property;
953-
* @Property("el-property") dirProperty;
945+
* class BankAccount {
946+
* @Property() bankName: string;
947+
* @Property('account-id') id: string;
948+
*
949+
* // this property is not bound, and won't be automatically updated by Angular
950+
* normalizedBankName: string;
954951
* }
952+
*
953+
* @Component({selector: 'app'})
954+
* @View({
955+
* template: `
956+
* <bank-account bank-name="RBC" account-id="4747"></bank-account>
957+
* `,
958+
* directives: [BankAccount]
959+
* })
960+
* class App {}
961+
*
962+
* bootstrap(App);
955963
* ```
956964
*/
957965
@CONST()
958966
export class PropertyMetadata {
959-
constructor(public bindingPropertyName?: string) {}
967+
constructor(
968+
/**
969+
* Name used when instantiating a component in the temlate.
970+
*/
971+
public bindingPropertyName?: string) {}
960972
}
961973

962974
/**
963-
* Declare a bound event.
975+
* Declares an event-bound property.
964976
*
965-
* ## Example
977+
* When an event-bound property emits an event, an event handler attached to that event
978+
* the template is invoked.
966979
*
967-
* ```
980+
* `EventMetadata` takes an optional parameters that specifies that name
981+
* used when instantiating a component in the template. When not provided,
982+
* the class property name is used.
983+
*
984+
* ### Example
985+
*
986+
* ```typescript
968987
* @Directive({
969-
* selector: 'sample-dir'
988+
* selector: 'interval-dir',
970989
* })
971-
* class SampleDir {
972-
* @Event() event = new EventEmitter(); // Same as @Event('event') event = new EventEmitter();
973-
* @Event("el-event") dirEvent = new EventEmitter();
990+
* class IntervalDir {
991+
* @Event() everySecond = new EventEmitter();
992+
* @Event('everyFiveSeconds') five5Secs = new EventEmitter();
993+
*
994+
* constructor() {
995+
* setInterval(() => this.everySecond.next("event"), 1000);
996+
* setInterval(() => this.five5Secs.next("event"), 5000);
997+
* }
974998
* }
999+
*
1000+
* @Component({selector: 'app'})
1001+
* @View({
1002+
* template: `
1003+
* <interval-dir (every-second)="everySecond()" (every-five-seconds)="everyFiveSeconds()">
1004+
* </interval-dir>
1005+
* `,
1006+
* directives: [IntervalDir]
1007+
* })
1008+
* class App {
1009+
* everySecond() { console.log('second'); }
1010+
* everyFiveSeconds() { console.log('five seconds'); }
1011+
* }
1012+
* bootstrap(App);
9751013
* ```
9761014
*/
9771015
@CONST()
@@ -980,9 +1018,11 @@ export class EventMetadata {
9801018
}
9811019

9821020
/**
983-
* Declare a host property binding.
1021+
* Declares a host binding property.
9841022
*
985-
* ## Example
1023+
* Angular automatically updates data-bound properties during change detection.
1024+
*
1025+
* ### Example
9861026
*
9871027
* ```
9881028
* @Directive({

0 commit comments

Comments
 (0)