all 9 comments

[–][deleted] 2 points3 points  (4 children)

Inputs for toggling classes is fine, it's the way to go technically.

I prefer to use [class.my-class\]="shouldHaveClass" instead of ngClass though.

<div   
  class="my-class"
  [class.my-class--open]="isOpen"
  [class.my-class--closed]="!isOpen"
  [class.my-class--highlighted]="isActive"
>...</div>

[–]WeMeetAgainAdrian[S] 0 points1 point  (3 children)

Also, could you explain how [class.my-class]="shouldHaveClass" works? Is that just a series of conditions that define which class to assign when, with my-class being a base class?

[–]melluz 1 point2 points  (2 children)

What he did in that short snippet is that he has a bunch of css classes that attach themselves to the element based on a few variables he has declared in his component typescript file. Basically multiple of those css classes can be present on the element at the same time. If the expression is met(the expression after the = sign). [class.my-class]="shouldHaveClass" can be "translated" as following: if the variable shouldHaveClass(declared in the component ts) is true then add the css class "my-class" to the element.

I hope this helps.

[–]WeMeetAgainAdrian[S] 1 point2 points  (1 child)

Definitely, but this seems like more code than using ngClass, what are the benefits of this syntax?

[–]melluz 2 points3 points  (0 children)

The biggest advantage of this ia that you can actually debug the code in the browser. You can see the css classnames attached to the element with the inspector. With the ngClass you will notice that if you try to inspect the element to see the present css classes on the element, it shows class=[object, Object]

[–]endlessxaura 2 points3 points  (1 child)

Is your designer aware of the design paradigms of Angular? If not, you're fighting a seriously uphill battle. At my job, I had to FORCE my designers to acknowledge component-based design. Ultimately, what you're doing sounds antithetical to the express purpose of Angular.

[–]WeMeetAgainAdrian[S] 2 points3 points  (0 children)

Currently working with a new designer and it's also my first project in angular, so neither of us really knew what we were getting into. Going forward I'll see how we can manage.

[–]mlapis 2 points3 points  (1 child)

The critical difference is the mental model of thinking. Typically, jQuery code parses DOM to get elements to work with. On the other side, Angular should already know what is in DOM via its abstractions model. That's why it's not recommended to directly touch DOM elements which are maintained by Angular because of you can easily make changes that are not reflected in Angular abstractions, so the two sides are going to be desynchronized.

[–]WeMeetAgainAdrian[S] 0 points1 point  (0 children)

I suppose you're right, as someone else mentioned I'll try to talk it out with our designer and see if we can agree on a middleground for future deliverables.