Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@schematics/angular): use signal in app component #29109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('AppComponent', () => {
it(`should have as title '<%= name %>'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('<%= name %>');
expect(app.title()).toEqual('<%= name %>');
});

it('should render title', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Component } from '@angular/core';
import { Component, signal } from '@angular/core';

@Component({
selector: '<%= selector %>',<% if(inlineTemplate) { %>
template: `
<h1>Welcome to {{title}}!</h1>
<h1>Welcome to {{ title() }}!</h1>

<% if (routing) {
%><router-outlet /><%
Expand All @@ -15,5 +15,5 @@ import { Component } from '@angular/core';
styleUrl: './app.component.<%= style %>'<% } %>
})
export class AppComponent {
title = '<%= name %>';
title = signal('<%= name %>');
}
Copy link
Collaborator

@alan-agius4 alan-agius4 Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it logical for this to be considered a signal, given that it's just a static string? It's feels like this would be a bad usage of signals.

PS: In case we go forward with this we need to update of a couple of other templates.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it really is a static string, then using it directly in the template would make more sense than having a dedicated field no? I always saw it as an example of a dynamic string that the code would update.

Let me know if that looks interesting and I'll update the templates I missed 👍

Copy link
Collaborator

@alan-agius4 alan-agius4 Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not too strongly about this, but I wonder if it would send the wrong "signal", that anything referenced in the template needs to be a signal. I'll not directly in the template as it's easier to access in tests.

I'll defer to @alxhub. An alternative for correctness maybe I could see this title having the readonly modifier added.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('AppComponent', () => {
it(`should have the '<%= name %>' title`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('<%= name %>');
expect(app.title()).toEqual('<%= name %>');
});

it('should render title', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Component } from '@angular/core';<% if(routing) { %>
import { Component, signal } from '@angular/core';<% if(routing) { %>
import { RouterOutlet } from '@angular/router';<% } %>

@Component({
selector: '<%= selector %>',
imports: [<% if(routing) { %>RouterOutlet<% } %>],<% if(inlineTemplate) { %>
template: `
<h1>Welcome to {{title}}!</h1>
<h1>Welcome to {{ title() }}!</h1>

<% if (routing) {
%><router-outlet /><%
Expand All @@ -16,5 +16,5 @@ import { RouterOutlet } from '@angular/router';<% } %>
styleUrl: './app.component.<%= style %>'<% } %>
})
export class AppComponent {
title = '<%= name %>';
title = signal('<%= name %>');
}
Loading