import {Component, Directive, ElementRef, ViewChild} from 'angular2/core';
@Directive({
selector: '[focusOnInit]'
})
export class FocusOnInit {
constructor(private elementRef: ElementRef) {}
ngAfterViewInit() {
this.elementRef.nativeElement.focus();
}
}
@Component({
selector: 'my-app',
template: `<h1>My First Angular 2 App</h1>
<button (click)="appearInput()" [hidden]="isVisible">Make it visible</button>
<input #input [hidden]="!isVisible" focusOnInit>
<button (click)="focusInput(input)" [hidden]="!isVisible">Focus it</button>
<button (click)="appearInput(false)" [hidden]="!isVisible">Make it invisible</button>
`,
directives:[FocusOnInit]
})
export class AppComponent {
private isVisible = true;
appearInput(visible){
this.isVisible = visible !== false;
}
focusInput(element: any){
element.focus();
}
}
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './app.component'
bootstrap(AppComponent);
beforeEach(() => {
jasmine.addMatchers({
toContainText: function() {
return {
compare: function(actual: any, expectedText: string) {
var actualText = actual.textContent;
return {
pass: actualText.indexOf(expectedText) > -1,
get message() { return 'Expected ' + actualText + ' to contain ' + expectedText; }
};
}
};
}
});
});
<!DOCTYPE html>
<html>
<head>
<title>Angular 2 QuickStart</title>
<!-- 1. Load libraries -->
<script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.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>
<!-- 2. Configure SystemJS -->
<script>
System.config({
transpiler: 'typescript',
typescriptOptions: { emitDecoratorMetadata: true },
packages: {'app': {defaultExtension: 'ts'}}
});
System.import('app/boot')
.then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
Copyright 2010-2015 Google, Inc. http://angularjs.org
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.