Sign in

Justin

@iamjustin.dev
317 followers 433 following 48 posts

Front-end dev. Mostly Angular. I write about it as well! medium.com/@iamjustin

PostsRepliesMedia
Justin @iamjustin.dev · 09/12/2024
Why is the `NgModelGroup` not available without a `setTimeout`, but the `NgControl` is? Guess I'm gonna have to dive into the source x) #angular
@Component({
  standalone: true,
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [FormsModule],
  selector: 'app-group',
  template: `
    <form>
      <div #personGroup="ngModelGroup" ngModelGroup="person">
        <input
          type="text"
          [(ngModel)]="formModel().name"
          name="name"
          #nameControl="ngModel"
        />
      </div>
    </form>
  `,
})
export class GroupComponent {
  personGroup = viewChild.required<NgModelGroup>('personGroup');
  nameControl = viewChild.required<NgControl>('nameControl');

  formModel = signal({
    name: 'John',
  });

  ngAfterViewInit() {
    this.nameControl().valueChanges!.subscribe((value) => {
      console.log('name', value);
    });

    setTimeout(() => {
      this.personGroup().valueChanges!.subscribe((value) => {
        console.log('personGroup', value);
      });
    });
  }
}
130
Justin @iamjustin.dev · 22/11/2024
Experimenting with TDF, but how do we react to `valueChanges` declaratively? This works, but idk #Angular
Screenshot of TypeScript/Angular code showing an AddressInfoComponent class. The code includes a form template with a company ID input field and an address display section. The component class contains dependency injections and an Observable pipe chain that processes address data based on company ID validation.
120
Justin @iamjustin.dev · 17/11/2024
Is this cursed? Add template narrowing with the new `@let` syntax, but use a `computed` instead of the `input` directly because of the reactive context. #angular
type Post = {
  type: 'post';
  title: string;
};

type Todo = {
  type: 'todo';
  completed: boolean;
};

type Book = {
  type: 'book';
  author: string;
};

type Entry = Post | Todo | Book;

@Component({
  ...
  template: `
    @let entry = this.currentEntry();

    @if (entry.type === 'post') {
      {{ entry.title }}
    }

    @if (entry.type === 'todo') {
      {{ entry.completed }}
    }

    @if (entry.type === 'book') {
      {{ entry.author }}
    }
  `,
})
export class EntryComponent {
  entry = input.required<Entry>();
  currentEntry = computed(() => this.entry());
}
300
Justin @iamjustin.dev · 13/11/2024
What if you could dynamically change the mock responses within your #Angular app (or any framework, really). Prototyped something and it actually works!
062
Justin @iamjustin.dev · 12/11/2024
`outputFromObservable` is pretty cool. #Angular
Code of a random input component that utilizes outputFromObservable to trigger an emit with the field value when the field is valid.
020