2

New to Angular so be nice...

I have a component that get a date/string value from a parent component:

export class myComponent implements OnInit {
    @Input() myDate: string;

    constructor(){}

    ngOnInit(){}
}

The problem I have is that I want to use split() method on the @Input myDate string so that I can use the parts (array indexes assigned to vars) in the view individually.

I tried following the Angular guide and ended up with this in my class:

private _myDate;
@Input()
set myDate(myDate: string){
    this._myDate = (myDate && myDate.split(' '));
}
get myDate(): string { return this._myDate };

but that returns undefined cannot and I don't know if the code I need to write goes in the class or in the constructor (because I see conflicting info all over the web).

Variation on above returns "Property split does not exist on type 'String[]'" which akes sense, but I can't seem to find out how to avoid either that error or in the first code sample, cannot convert type string to array:

private _myDate = [];
@Input()
set myDate(myDate: Array<String>){
    this._myDate = (myDate && myDate.split(' '));
}
get myDate(): Array<String> { return this._myDate };

So, how do I do work on the @Input myDate value and where do I do it?

6
  • Doesn't this (myDate && myDate.split(' ')) return a boolean response though? Commented Aug 21, 2018 at 19:06
  • How does the value look like, which you're given into the component? And what do you want to do with the array and its indexes? Commented Aug 21, 2018 at 19:20
  • Anjil I don't know in all honesty, I was following the google/angular getting started guide and short on time. Sorry. @Batajus the value I import is a string formatted like "12 May, 2017" and I want to split it it into datePart[0]=12, datePart[1]=May, datePart[2]= 2017 to call each datePart in the template separately. No idea if going about this the right way. Any advice is received with gratitude :) Commented Aug 21, 2018 at 19:23
  • in your children component you can write e.g. {{myDate[0]}}. but I think that you needn't use this._myDate=(myDate && myDate.split('')). I think you want to say this._myDate=myDate? myDate.split(' '):[]. NOTE.declare the type of the variable private _myDate:string[]=[]; Commented Aug 21, 2018 at 19:25
  • @AnjilDhamala No, in JavaScript using && on non-booleans returns either the first falsy value or the last value. In this case it'll return myDate if it's null/undefined/empty, otherwise it'll return the result of split. Commented Aug 21, 2018 at 19:35

3 Answers 3

2

To achieve your wished goal you can do follow:

private _myDate: string[];

@Input() 
set myDate(myDate: string){ // You're receiving a string, so the correct typing is string and not Array<string>

    // For a clean 'string' you need to remove the , from your given value
    let cleanDate: string = myDate.replace(',', ''); 

    // After you've removed the , you can simply split the string to get your array
    this._myDate = cleanDate.split(' ');
}

get myDate(): string { return this._myDate };

I hope my comments are understandable ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

I used this answer with a couple of small changes. Firstly, _myDate I had to set to public in the first line (private threw an error in the linter), and the getter didn't work for me at all, throwing an error there too (not at that PC so can't remember what it was, sorry). Thanks @Batajus
1

You can use it inside ngOnChanges

 ngOnChanges(changes: SimpleChanges) {
   if (changes['myDate']) {
     this._myDate = (myDate && myDate.split(' '));
   }
 }

1 Comment

it's not necesary, the use of setter in input must be get the job
1

I normally don't use a setter but I can assume you can just do this and achieve what you want.

<your-component-selector [myDate]="somePropertyInParentClass"></your-component-selector>

Child component would look something like this:

export class YourComponent implements AfterViewInit {
  @Input() myDate: any;

  ngAfterViewInit() {
    this.myDate = this.myDate.split(" ");
  }
}

After that, just loop over your myDate prop using *ngFor in your view.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.