What I'm trying to do is show the full name when the first and last name are entered.
It works:
<h1 [hidden]="!firstName || !lastName">Hello {{lastName + ', ' + firstName}}!</h1>
This does not work (what is the correct way to call the method defined in the class?):
This also does not work:
<button ng-click="alertFullName()">show full name</button>
Here are the files: index.html:
<!DOCTYPE html> <html> <head> <title>Angular 2 QuickStart</title> <script src="https://rawgithub.com/systemjs/systemjs/0.19.6/dist/system.js"></script> <script src="https://code.angularjs.org/tools/typescript.js"></script> <script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script> <script src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script> <script src="https://code.angularjs.org/2.0.0-beta.0/angular2.dev.js"></script> <script> System.config({ transpiler: 'typescript', typescriptOptions: { emitDecoratorMetadata: true }, packages: {'src': {defaultExtension: 'ts'}} }); </script> <script> System.import('angular2/platform/browser').then(function(ng){ System.import('src/hello_world').then(function(src) { ng.bootstrap(src.HelloWorld); }); }); </script> </head> <body> <hello-world>Loading...</hello-world> </body> </html>
SIC / hello_world.html:
<label>Name:</label> <input type="text" [(ngModel)]="firstName" placeholder="Enter first name here"> <input type="text" [(ngModel)]="lastName" placeholder="Enter last name here"> <hr> <h1 [hidden]="!firstName || !lastName">Hello {{lastName + ', ' + firstName}}!</h1> <button ng-click="alertFullName()">show full name</button>
SRC / hello_world.ts:
import {Component} from 'angular2/core'; @Component({ // Declare the tag name in index.html to where the component attaches selector: 'hello-world', // Location of the template for this component templateUrl: 'src/hello_world.html' }) export class HelloWorld { firstName: string = ''; lastName: string = ''; function fullName(): string { return firstName + ', ' + lastName; } function alertFullName() { alert(firstName + ', ' + lastName); } }
source share