Angularは、開発者が高速で信頼性の高いアプリケーションを構築できるようにするWebフレームワークです。 Googleの専門チームによって管理されているAngularは、 開発ワークフローを簡素化し効率化するためのツール、API、ライブラリの幅広いスイートを提供します。 Angularは、チームの規模やコードベースの規模に合わせて拡張できる、 高速で信頼性の高いアプリケーションを構築するための堅牢なプラットフォームを提供します。 **コードを確認したいですか?** Angularの使い方の概要を簡単に確認するには、 [基本要素](essentials)にアクセスしてください。 ステップバイステップの手順に従いたい場合は、[チュートリアル](tutorials/learn-angular)で始めることができます。 ## 開発を強力にサポートする機能 ## これまでにない開発速度 ## 自信を持ってリリース ## あらゆる規模で機能する ## オープンソースファースト ## 活気のあるコミュニティ オンラインスターターを使ってAngularをすぐに始めましょう。または、ターミナルを使ってローカルでも開始できます。 ## オンラインで試す プロジェクトを設定せずに、ブラウザでAngularを試したい場合は、オンラインサンドボックスを使うことができます。 ## ローカルに新しいプロジェクトを設定する 新しいプロジェクトを開始する場合、Gitなどのツールを使用するために、ローカルにプロジェクトを作成するのが一般的です。 ### 前提条件 - **Node.js** - [v20.11.1以降](/reference/versions) - **テキストエディタ** - [Visual Studio Code](https://code.visualstudio.com/)を推奨 - **ターミナル** - Angular CLIコマンドを実行するために必要 - **開発ツール** - 開発ワークフローを改善するために、[Angular Language Service](/tools/language-service) をおすすめします ### 手順 以下のガイドは、ローカルにAngularプロジェクトを設定する手順を説明します。 #### Angular CLIをインストールする ターミナルを開き([Visual Studio Code](https://code.visualstudio.com/)を使用している場合は、[統合ターミナル](https://code.visualstudio.com/docs/editor/integrated-terminal)を開くことができます)、次のコマンドを実行します。 ``` // npm npm install -g @angular/cli ``` ``` // pnpm pnpm install -g @angular/cli ``` ``` // yarn yarn global add @angular/cli ``` ``` // bun bun install -g @angular/cli ``` WindowsまたはUnixでこのコマンドを実行する際に問題が発生した場合は、[CLIドキュメント](/tools/cli/setup-local#install-the-angular-cli)を参照してください。 #### 新しいプロジェクトを作成する ターミナルで、CLIコマンド`ng new`を実行し、目的のプロジェクト名を入力します。次の例では、`my-first-angular-app`というプロジェクト名を使用します。 ```shell ng new ``` プロジェクトの設定に関するいくつかのオプションが表示されます。矢印キーとEnterキーを使ってナビゲートし、必要なオプションを選択します。 特に好みがなければ、Enterキーを押してデフォルトのオプションを採用し、設定を続行してください。 設定オプションを選択し、CLIがセットアップを実行すると、次のようなメッセージが表示されます。 ```shell ✔ Packages installed successfully. Successfully initialized git. ``` これで、ローカルにプロジェクトを実行する準備が整いました! #### ローカルに新しいプロジェクトを実行する ターミナルで、新しいAngularプロジェクトに切り替えます。 ```shell cd my-first-angular-app ``` この時点で、すべての依存関係がインストールされているはずです(プロジェクト内に`node_modules`フォルダが存在することを確認できます)。次のコマンドを実行してプロジェクトを開始できます。 ```shell npm start ``` すべてが正常に完了すると、ターミナルに次のような確認メッセージが表示されます。 ```shell Watch mode enabled. Watching for file changes... NOTE: Raw file sizes do not reflect development server per-request transformations. ➜ Local: http://localhost:4200/ ➜ press h + enter to show help ``` これで、`Local`のパス(例:`http://localhost:4200`)にアクセスしてアプリケーションを確認できます。コーディングを楽しんでください!🎉 ### 開発でのAIの使用 AI搭載IDEでの開発を開始するには、[Angularプロンプトルールとベストプラクティスをチェックしてください](/ai/develop-with-ai)。 ## 次のステップ Angularプロジェクトを作成したので、[基本概念ガイド](/essentials)でAngularの詳細について学ぶか、詳しいガイドからトピックを選択してください! # Angular コーディングスタイルガイド ## Introduction This guide covers a range of style conventions for Angular application code. These recommendations are not required for Angular to work, but instead establish a set of coding practices that promote consistency across the Angular ecosystem. A consistent set of practices makes it easier to share code and move between projects. This guide does _not_ cover TypeScript or general coding practices unrelated to Angular. For TypeScript, check out [Google's TypeScript style guide](https://google.github.io/styleguide/tsguide.html). ### When in doubt, prefer consistency Whenever you encounter a situation in which these rules contradict the style of a particular file, prioritize maintaining consistency within a file. Mixing different style conventions in a single file creates more confusion than diverging from the recommendations in this guide. ## Naming ### Separate words in file names with hyphens Separate words within a file name with hyphens (`-`). For example, a component named `UserProfile` has a file name `user-profile.ts`. ### Use the same name for a file's tests with `.spec` at the end For unit tests, end file names with `.spec.ts`. For example, the unit test file for the `UserProfile` component has the file name `user-profile.spec.ts`. ### Match file names to the TypeScript identifier within File names should generally describe the contents of the code in the file. When the file contains a TypeScript class, the file name should reflect that class name. For example, a file containing a component named `UserProfile` has the name `user-profile.ts`. If the file contains more than one primary namable identifier, choose a name that describes the common theme to the code within. If the code in a file does not fit within a common theme or feature area, consider breaking the code up into different files. Avoid overly generic file names like `helpers.ts`, `utils.ts`, or `common.ts`. ### Use the same file name for a component's TypeScript, template, and styles Components typically consist of one TypeScript file, one template file, and one style file. These files should share the same name with different file extensions. For example, a `UserProfile` component can have the files `user-profile.ts`, `user-profile.html`, and `user-profile.css`. If a component has more than one style file, append the name with additional words that describe the styles specific to that file. For example, `UserProfile` might have style files `user-profile-settings.css` and `user-profile-subscription.css`. ## Project structure ### All the application's code goes in a directory named `src` All of your Angular UI code (TypeScript, HTML, and styles) should live inside a directory named `src`. Code that's not related to UI, such as configuration files or scripts, should live outside the `src` directory. This keeps the root application directory consistent between different Angular projects and creates a clear separation between UI code and other code in your project. ### Bootstrap your application in a file named `main.ts` directly inside `src` The code to start up, or **bootstrap**, an Angular application should always live in a file named `main.ts`. This represents the primary entry point to the application. ### Group closely related files together in the same directory Angular components consist of a TypeScript file and, optionally, a template and one or more style files. You should group these together in the same directory. Unit tests should live in the same directory as the code-under-test. Avoid collecting unrelated tests into a single `tests` directory. ### Organize your project by feature areas Organize your project into subdirectories based on the features of your application or common themes to the code in those directories. For example, the project structure for a movie theater site, MovieReel, might look like this: ``` src/ ├─ movie-reel/ │ ├─ show-times/ │ │ ├─ film-calendar/ │ │ ├─ film-details/ │ ├─ reserve-tickets/ │ │ ├─ payment-info/ │ │ ├─ purchase-confirmation/ ``` Avoid creating subdirectories based on the type of code that lives in those directories. For example, avoid creating directories like `components`, `directives`, and `services`. Avoid putting so many files into one directory that it becomes hard to read or navigate. As the number of files in a directory grows, consider splitting further into additional sub-directories. ### One concept per file Prefer focusing source files on a single _concept_. For Angular classes specifically, this usually means one component, directive, or service per file. However, it's okay if a file contains more than one component or directive if your classes are relatively small and they tie together as part of a single concept. When in doubt, go with the approach that leads to smaller files. ## Dependency injection ### Prefer the `inject` function over constructor parameter injection Prefer using the `inject` function over injecting constructor parameters. The `inject` function works the same way as constructor parameter injection, but offers several style advantages: * `inject` is generally more readable, especially when a class injects many dependencies. * It's more syntactically straightforward to add comments to injected dependencies * `inject` offers better type inference. * When targeting ES2022+ with [`useDefineForClassFields`](https://www.typescriptlang.org/tsconfig/#useDefineForClassFields), you can avoid separating field declaration and initialization when fields read on injected dependencies. [You can refactor existing code to `inject` with an automatic tool](reference/migrations/inject-function). ## Components and directives ### Choosing component selectors See the [Components guide for details on choosing component selectors](guide/components/selectors#choosing-a-selector). ### Naming component and directive members See the Components guide for details on [naming input properties](guide/components/inputs#choosing-input-names) and [naming output properties](guide/components/outputs#choosing-event-names). ### Choosing directive selectors Directives should use the same [application-specific prefix](guide/components/selectors#selector-prefixes) as your components. When using an attribute selector for a directive, use a camelCase attribute name. For example, if your application is named "MovieReel" and you build a directive that adds a tooltip to an element, you might use the selector `[mrTooltip]`. ### Group Angular-specific properties before methods Components and directives should group Angular-specific properties together, typically near the top of the class declaration. This includes injected dependencies, inputs, outputs, and queries. Define these and other properties before the class's methods. This practice makes it easier to find the class's template APIs and dependencies. ### Keep components and directives focused on presentation Code inside your components and directives should generally relate to the UI shown on the page. For code that makes sense on its own, decoupled from the UI, prefer refactoring to other files. For example, you can factor form validation rules or data transformations into separate functions or classes. ### Avoid overly complex logic in templates Angular templates are designed to accommodate [JavaScript-like expressions](guide/templates/expression-syntax). You should take advantage of these expressions to capture relatively straightforward logic directly in template expressions. When the code in a template gets too complex, though, refactor logic into the TypeScript code (typically with a [computed](guide/signals#computed-signals)). There's no one hard-and-fast rule that determines what constitutes "complex". Use your best judgement. ### Use `protected` on class members that are only used by a component's template A component class's public members intrinsically define a public API that's accessible via dependency injection and [queries](guide/components/queries). Prefer `protected` access for any members that are meant to be read from the component's template. ```ts @Component({ ..., template: `

{{ fullName() }}

`, }) export class UserProfile { firstName = input(); lastName = input(); // `fullName` is not part of the component's public API, but is used in the template. protected fullName = computed(() => `${this.firstName()} ${this.lastName()}`); } ``` ### Use `readonly` on properties that are initialized by Angular Mark component and directive properties initialized by Angular as `readonly`. This includes properties initialized by `input`, `model`, `output`, and queries. The readonly access modifier ensures that the value set by Angular is not overwritten. ```ts @Component({/* ... */}) export class UserProfile { readonly userId = input(); readonly userSaved = output(); } ``` For components and directives that use the decorator-based `@Input`, `@Output`, and query APIs, this advice applies to output properties and queries, but not input properties. ```ts @Component({/* ... */}) export class UserProfile { @Output() readonly userSaved = new EventEmitter(); @ViewChildren(PaymentMethod) readonly paymentMethods?: QueryList; } ``` ### Prefer `class` and `style` over `ngClass` and `ngStyle` Prefer `class` and `style` bindings over using the [`NgClass`](/api/common/NgClass) and [`NgStyle`](/api/common/NgStyle) directives. ```html
``` Both `class` and `style` bindings use a more straightforward syntax that aligns closely with standard HTML attributes. This makes your templates easier to read and understand, especially for developers familiar with basic HTML. Additionally, the `NgClass` and `NgStyle` directives incur an additional performance cost compared to the built-in `class` and `style` binding syntax. For more details, refer to the [bindings guide](/guide/templates/binding#css-class-and-style-property-bindings) ### Name event handlers for what they _do_, not for the triggering event Prefer naming event handlers for the action they perform rather than for the triggering event: ```html ``` Using meaningful names like this makes it easier to tell what an event does from reading the template. For keyboard events, you can use Angular's key event modifiers with specific handler names: ```html