user13473845
user13473845

Reputation:

confusion over creating function in angular

Im new in Angular,in Jquery when you deal with a piece of code which you need to use again,normally we create a js file or function to call in once needed,in Angular i have a same problem but i was woundering if i should create a component for it or js or function,since im in the biggining i would like to know the best practice,here is my code:

  public valueChange(value: string): void {
              this.from= formatDate( this.value,'yyyy-MM-dd', 'en_US');
              this.to=formatDate( this.value1,'yyyy-MM-dd', 'en_US');
              this.Legendtemp.push(value);
              this.serisName= value;
              this.services.getWindAverage(value,this.from,this.to).subscribe(s=>{
              this.selected=s;
              this.series.push(this.selected);
              i=this.series.length-1;      
              })
              this.temp.push(value.length);
              }
     else{
              console.log("Dseleted");
              if(this.temp.length!=0){this.temp.pop();this.series.pop();this.temp.length}   
             }
             }

Its a multiselectBox gets the selected values push them into an array and pass to back end along with the date , now i have added a filter button somewhere else in this page, which should do the same thing,whats the best practise to do that in Angular?

Upvotes: 0

Views: 28

Answers (1)

Manish
Manish

Reputation: 6286

It's basically the same programming concept.

If you think a set of code could be reused, you separate it out into an entity (like, method, class etc) and reuse it wherever required.

Similarly in Angular, if you think a set of code (HTML and it's logic) could be reused and can be separated out as a generic component which could be reused elsewhere as well then go ahead and create a component.

But if it has a logic which cannot be generalized, it is better to use as is in the parent component without making a component out of it.

Also in Angular, if you want to only separate out and share the logic and/or data, use services. (Thanks Ben for mentioning that.)

Upvotes: 4

Related Questions