From e2fd62861892c3a31d52482e105465c78ccd925d Mon Sep 17 00:00:00 2001 From: Kapunahele Wong Date: Thu, 29 Nov 2018 12:49:29 -0500 Subject: [PATCH] docs: edit template ref vars copy and example (#27371) PR Close #27371 --- .../e2e/src/app.e2e-spec.ts | 62 ++++++++++++++++ .../example-config.json | 0 .../src/app/app.component.css | 0 .../src/app/app.component.html | 55 ++++++++++++++ .../src/app/app.component.spec.ts | 27 +++++++ .../src/app/app.component.ts | 31 ++++++++ .../src/app/app.module.ts | 22 ++++++ .../src/index.html | 14 ++++ .../template-reference-variables/src/main.ts | 11 +++ .../stackblitz.json | 6 ++ aio/content/guide/template-syntax.md | 71 ++++++++++--------- 11 files changed, 265 insertions(+), 34 deletions(-) create mode 100644 aio/content/examples/template-reference-variables/e2e/src/app.e2e-spec.ts create mode 100644 aio/content/examples/template-reference-variables/example-config.json create mode 100644 aio/content/examples/template-reference-variables/src/app/app.component.css create mode 100644 aio/content/examples/template-reference-variables/src/app/app.component.html create mode 100644 aio/content/examples/template-reference-variables/src/app/app.component.spec.ts create mode 100644 aio/content/examples/template-reference-variables/src/app/app.component.ts create mode 100644 aio/content/examples/template-reference-variables/src/app/app.module.ts create mode 100644 aio/content/examples/template-reference-variables/src/index.html create mode 100644 aio/content/examples/template-reference-variables/src/main.ts create mode 100644 aio/content/examples/template-reference-variables/stackblitz.json diff --git a/aio/content/examples/template-reference-variables/e2e/src/app.e2e-spec.ts b/aio/content/examples/template-reference-variables/e2e/src/app.e2e-spec.ts new file mode 100644 index 0000000000..3d742d5a97 --- /dev/null +++ b/aio/content/examples/template-reference-variables/e2e/src/app.e2e-spec.ts @@ -0,0 +1,62 @@ +'use strict'; // necessary for es6 output in node + +import { browser, element, by } from 'protractor'; +import { logging } from 'selenium-webdriver'; + +describe('Template-reference-variables-example', function() { + beforeEach(function() { + browser.get(''); + + }); + + // helper function used to test what's logged to the console + async function logChecker(button, contents) { + const logs = await browser + .manage() + .logs() + .get(logging.Type.BROWSER); + const message = logs.filter(({ message }) => + message.indexOf(contents) !== -1 ? true : false + ); + expect(message.length).toBeGreaterThan(0); + } + + it('should display Template reference variables', function() { + expect(element(by.css('h1')).getText()).toEqual( + 'Template reference variables' + ); + }); + + it('should log a Calling 123 ... message', async () => { + let callButton = element.all(by.css('button')).get(0); + let phoneInput = element.all(by.css('input')).get(0); + await phoneInput.sendKeys('123'); + await callButton.click(); + const contents = 'Calling 123 ...'; + await logChecker(callButton, contents); + }); + + it('should log a Faxing 123 ... message', async () => { + let faxButton = element.all(by.css('button')).get(1); + let faxInput = element.all(by.css('input')).get(1); + await faxInput.sendKeys('123'); + await faxButton.click(); + const contents = 'Faxing 123 ...'; + await logChecker(faxButton, contents); + }); + + it('should display a disabled button', function() { + let disabledButton = element.all(by.css('button')).get(2); + expect(disabledButton.isEnabled()).toBe(false); + }); + + it('should submit form', async () => { + let submitButton = element.all(by.css('button')).get(3); + let nameInput = element.all(by.css('input')).get(2); + await nameInput.sendKeys('123'); + await submitButton.click(); + expect(element.all(by.css('div > p')).get(2).getText()).toEqual('Submitted. Form value is {"name":"123"}'); + }); + + +}); diff --git a/aio/content/examples/template-reference-variables/example-config.json b/aio/content/examples/template-reference-variables/example-config.json new file mode 100644 index 0000000000..e69de29bb2 diff --git a/aio/content/examples/template-reference-variables/src/app/app.component.css b/aio/content/examples/template-reference-variables/src/app/app.component.css new file mode 100644 index 0000000000..e69de29bb2 diff --git a/aio/content/examples/template-reference-variables/src/app/app.component.html b/aio/content/examples/template-reference-variables/src/app/app.component.html new file mode 100644 index 0000000000..d335a33956 --- /dev/null +++ b/aio/content/examples/template-reference-variables/src/app/app.component.html @@ -0,0 +1,55 @@ +

Template reference variables

+ +
+

Pass value to an event handler

+

See console for output.

+ + + + + + + + + + +
+ +
+ + + + +
+ +
+ +
+

Template reference variable with disabled button

+

btn refers to the button element.

+ +
+ +
+ +

Reference variables, forms, and NgForm

+ +
+ + +
+ +
+

{{ submitMessage }}

+
+ + + + +

JSON: {{ itemForm.form.value | json }}

diff --git a/aio/content/examples/template-reference-variables/src/app/app.component.spec.ts b/aio/content/examples/template-reference-variables/src/app/app.component.spec.ts new file mode 100644 index 0000000000..bcbdf36b3e --- /dev/null +++ b/aio/content/examples/template-reference-variables/src/app/app.component.spec.ts @@ -0,0 +1,27 @@ +import { TestBed, async } from '@angular/core/testing'; +import { AppComponent } from './app.component'; +describe('AppComponent', () => { + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ + AppComponent + ], + }).compileComponents(); + })); + it('should create the app', async(() => { + const fixture = TestBed.createComponent(AppComponent); + const app = fixture.debugElement.componentInstance; + expect(app).toBeTruthy(); + })); + it(`should have as title 'app'`, async(() => { + const fixture = TestBed.createComponent(AppComponent); + const app = fixture.debugElement.componentInstance; + expect(app.title).toEqual('app'); + })); + it('should render title in a h1 tag', async(() => { + const fixture = TestBed.createComponent(AppComponent); + fixture.detectChanges(); + const compiled = fixture.debugElement.nativeElement; + expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!'); + })); +}); diff --git a/aio/content/examples/template-reference-variables/src/app/app.component.ts b/aio/content/examples/template-reference-variables/src/app/app.component.ts new file mode 100644 index 0000000000..a3546121df --- /dev/null +++ b/aio/content/examples/template-reference-variables/src/app/app.component.ts @@ -0,0 +1,31 @@ +import { Component, ViewChild } from '@angular/core'; +import { NgForm } from '@angular/forms'; + + +@Component({ + selector: 'app-root', + templateUrl: './app.component.html', + styleUrls: ['./app.component.css'] +}) +export class AppComponent { + @ViewChild('itemForm', { static: false }) form: NgForm; + + private _submitMessage = ''; + + get submitMessage() { + return this._submitMessage; + } + + onSubmit(form: NgForm) { + this._submitMessage = 'Submitted. Form value is ' + JSON.stringify(form.value); + } + + callPhone(value: string) { + console.warn(`Calling ${value} ...`); + } + + callFax(value: string) { + console.warn(`Faxing ${value} ...`); + } + +} diff --git a/aio/content/examples/template-reference-variables/src/app/app.module.ts b/aio/content/examples/template-reference-variables/src/app/app.module.ts new file mode 100644 index 0000000000..003e3adea3 --- /dev/null +++ b/aio/content/examples/template-reference-variables/src/app/app.module.ts @@ -0,0 +1,22 @@ +import { BrowserModule } from '@angular/platform-browser'; +import { NgModule } from '@angular/core'; + +import { FormsModule } from '@angular/forms'; + + + +import { AppComponent } from './app.component'; + + +@NgModule({ + declarations: [ + AppComponent + ], + imports: [ + BrowserModule, + FormsModule + ], + providers: [], + bootstrap: [AppComponent] +}) +export class AppModule { } diff --git a/aio/content/examples/template-reference-variables/src/index.html b/aio/content/examples/template-reference-variables/src/index.html new file mode 100644 index 0000000000..798bd31502 --- /dev/null +++ b/aio/content/examples/template-reference-variables/src/index.html @@ -0,0 +1,14 @@ + + + + + Template Reference Variables Example + + + + + + + Loading... + + diff --git a/aio/content/examples/template-reference-variables/src/main.ts b/aio/content/examples/template-reference-variables/src/main.ts new file mode 100644 index 0000000000..a9ca1caf8c --- /dev/null +++ b/aio/content/examples/template-reference-variables/src/main.ts @@ -0,0 +1,11 @@ +import { enableProdMode } from '@angular/core'; +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; + +import { AppModule } from './app/app.module'; +import { environment } from './environments/environment'; + +if (environment.production) { + enableProdMode(); +} + +platformBrowserDynamic().bootstrapModule(AppModule); diff --git a/aio/content/examples/template-reference-variables/stackblitz.json b/aio/content/examples/template-reference-variables/stackblitz.json new file mode 100644 index 0000000000..b683ad18b9 --- /dev/null +++ b/aio/content/examples/template-reference-variables/stackblitz.json @@ -0,0 +1,6 @@ +{ + "description": "Template Reference Variables", + "files": ["!**/*.d.ts", "!**/*.js", "!**/*.[1,2].*"], + "file": "src/app/app.component.ts", + "tags": ["Template Reference Variables"] +} diff --git a/aio/content/guide/template-syntax.md b/aio/content/guide/template-syntax.md index 269f9ca954..b4c30e6ed5 100644 --- a/aio/content/guide/template-syntax.md +++ b/aio/content/guide/template-syntax.md @@ -1686,70 +1686,73 @@ For example, you could replace the `` switch case with the follow {@a template-reference-variable} +{@a template-reference-variables--var-} + {@a ref-vars} {@a ref-var} -## Template reference variables ( #var ) +## Template reference variables (`#var`) A **template reference variable** is often a reference to a DOM element within a template. -It can also be a reference to an Angular component or directive or a -web component. +It can also refer to a directive (which contains a component), an element, [TemplateRef](api/core/TemplateRef), or a web component. + +For a demonstration of the syntax and code snippets in this section, see the template reference variables example. + Use the hash symbol (#) to declare a reference variable. -The `#phone` declares a `phone` variable on an `` element. +The following reference variable, `#phone`, declares a `phone` variable on an `` element. - + -You can refer to a template reference variable _anywhere_ in the template. -The `phone` variable declared on this `` is -consumed in a `