Giter Club home page Giter Club logo

Comments (5)

barretodavid avatar barretodavid commented on August 12, 2024 2

That is better but I think I wouldn't talk about the special <template> tag just yet to not confuse people.

@Component({
  selector: 'app-root',
  template: `
    <button (click)="toggleShow()">{{ show? 'hide': 'show' }}</button>
    <span *ngIf="show">{{ someString }}</span>`,
})
export class AppComponent {
  show = true;
  someString = 'What if you could conditionally show components?';
  toggleShow() {
    this.show = !this.show;
  };
}

from angular-training-examples.

bennett000 avatar bennett000 commented on August 12, 2024 1
  • Template tags should definitely come later.
  • The logic was in the *ngIf to illustrate the flexibility of the templating system. Although that is technically bad practice so let's go with the boolean solution.

from angular-training-examples.

xorgy avatar xorgy commented on August 12, 2024

It seems like it obscures the meaning and introduces more code. I understand the desire to separate concerns, but I don't think it helps in this case. There are cases where the logic is more than a simple expression; I would support using a method in those cases.

In addition, you then need to think of a method name for isVisible, introducing more work and giving people the opportunity to accidentally mislead the next person who comes along by naming it incorrectly.

I am, however, resistant to putting mutating logic in the template. For example: I wouldn't toggle the show state in the (click) expression.

from angular-training-examples.

xorgy avatar xorgy commented on August 12, 2024

The example is a bit contrived, though. I would instead do something like:

@Component({
  selector: 'app-root',
  template: `<button (click)="toggleShow()">{{ show? 'hide': 'show' }}</button>
    <template [ngIf]="show">{{ someString }}</template>`,
})
export class AppComponent {
  show = true;
  someString = 'What if you could conditionally show components?';
  toggleShow() {
    this.show = !this.show;
  };
}

from angular-training-examples.

xorgy avatar xorgy commented on August 12, 2024

You would definitely have a better idea of that. :- )

from angular-training-examples.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.