Trying to use multiple method( this.test(), this.flag = true ) inside ternary operator if condition success. But not working. Getting error like:
':' expected.
public core = [1];
public mare = [2];
public flag:boolean = false;
public msg:string;
ngOnInit() {
this.core.length == 1 && this.mare.length == 1 ? this.msg = 'Done', this.flag = true : '';
}
test() {
console.log('Done');
console.log()
}
Demo: https://stackblitz.com/edit/angular-ivy-n7txpp?file=src/app/app.component.ts
CodePudding user response:
Don't abuse ternary operators - they are not a replacement for if
statements. They should only be used in extremely simple situations. Using if
statements makes your code cleaner and more maintainable.
if(this.core.length == 1 && this.mare.length == 1){
this.msg = 'Done';
this.flag = true;
}
CodePudding user response:
Use ()
this.core.length == 1 && this.mare.length == 1 ? (this.test(), this.flag = true) : '';
or move this.flag=true
into test
Though as pointed out in comments don't
Why are you using the ternary conditional operator? You're not assigning anything. Just use an if statement/block. if (this.core.length == 1 && this.mare.length == 1) { this.test(); this.flag = true; }