1

I have json:

  public example=[{
       array:[item1, item2,]
    },
    {
       array:[item1, item2,]
    }]

file html:

<div *ngFor="let item of example">
      <div *ngIf="check(item.array)"> ...</div>
</div>

but call function check inside ngOnInit(), it error

 check(...request: string[]){
        return this._RolesService.checkRoles(request);
    }

ngOnInit(): void {
       let req:string[] = ['string1'];
        if(this.check(req)){}
}

gender html is ok, so When i call function check() in ngOnInit, it had error: Argument of type 'string[]' is not assignable to parameter of type 'string'.

2
  • i found anwser. Three dots # vs arguments. i change ngOnInit(): void { if(this.check('string1')){} } Commented May 31, 2022 at 9:58
  • If you found the solution please accept one of the answers or post your own answer to close the issue, do not hide the solution in the comments. Commented May 31, 2022 at 10:02

2 Answers 2

0

You should change your code into:

let req:string[] = ['string1'];
if(this.check(...req)){}

Or

let req = 'string1';
if(this.check(req)){}
Sign up to request clarification or add additional context in comments.

Comments

0

Can you please share the code of this._RolesService.checkRoles(request)? I think this function accepts a string as parameter, but you are passing ['string1'] which is an array of string. That is why the error is thrown.

Based on the current info you have provided, I think this is the issue.

Corrected solution: Thanks to the comments under this solution, one of the ways to solve your issue is to use the spread operator when calling the method:

this.check(...req)

3 Comments

public check(roles: string[]): boolean { var userRole = this._AuthenticationService.currentUserValue.userRole; return userRole?.some(e => roles?.includes(e.role.code +"")); }
I think "three dots" inside function check.
The spread operator inside a function is when you call to the function with parameters. typical sum(...values:number[]), and you use sum(1,2,3,4). -see you call to the function with a number of parameters -in the e.g. 4-. You can see as "rest of parameters", see, e.g. this link. In your example works if you call to the function like:this.check('string1')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.