Angular custom control - star rating - value from input









up vote
1
down vote

favorite












<div class="rating">
<div style="display: inline-block"
*ngFor="let starred of stars; let i = index"
(click)="rate(i + (starred ? (value > i + 1 ? 1 : 0) : 1))">
<ng-container *ngIf="starred; else noStar"><mat-icon class="filled">star</mat-icon></ng-container>
<ng-template #noStar><mat-icon class="empty">star_outline</mat-icon></ng-template>
</div>
</div>


@Component(
selector: 'jfg-star-rating',
templateUrl: './star-rating.component.html',
styleUrls: ['./star-rating.component.scss'],
providers: [

provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => StarRatingComponent),
multi: true

]

)
export class StarRatingComponent implements ControlValueAccessor

stars: boolean = Array(3).fill(true);

// Allow the input to be disabled, and when it is make it somewhat transparent.
@Input() disabled = false;
@HostBinding('style.opacity')
get opacity()
return this.disabled ? 1 : 1;


// Function to call when the rating changes.
onChange = (rating: number) =>
;

// Function to call when the input is touched (when a star is clicked).
onTouched = () =>
;


get value(): number
if(!this.disabled)
return this.stars.reduce((total, starred) =>
return total + (starred ? 1 : 0);
, 0);


rate(rating: number)
if (!this.disabled)
this.writeValue(rating);



// Allows Angular to update the model (rating).
// Update the model and changes needed for the view here.
writeValue(rating: number): void
if (!this.disabled)
this.stars = this.stars.map((_, i) => rating > i);
this.onChange(this.value);




// Allows Angular to register a function to call when the model (rating) changes.
// Save the function as a property to call later here.
registerOnChange(fn: (rating: number) => void): void
this.onChange = fn;


// Allows Angular to register a function to call when the input has been touched.
// Save the function as a property to call later here.
registerOnTouched(fn: () => void): void
this.onTouched = fn;


// Allows Angular to disable the input.
setDisabledState(isDisabled: boolean): void
this.disabled = isDisabled;






Im trying to make a component for star rating. I made it to work properly as an input. So i can press on the stars to get the correct value as a formControl and pass it to my services. But my problem is that i have tried to make my component to get value as an @input and set number of stars based on that value. I have tried inputing the value and setting it everywhere i could but still had no effect. If you could suggest me how can i proceed to set value form the input i would be glad :)










share|improve this question























  • The only input in that is the disabled property?
    – user184994
    Nov 11 at 11:15










  • Beforeu, is a custom form control component. So, the logic is that work as any input, you can use < star-ratting [(ngModel)]="your_variable" /> or < star-ratting fromControlName="myControl" />
    – Eliseo
    Nov 11 at 11:23














up vote
1
down vote

favorite












<div class="rating">
<div style="display: inline-block"
*ngFor="let starred of stars; let i = index"
(click)="rate(i + (starred ? (value > i + 1 ? 1 : 0) : 1))">
<ng-container *ngIf="starred; else noStar"><mat-icon class="filled">star</mat-icon></ng-container>
<ng-template #noStar><mat-icon class="empty">star_outline</mat-icon></ng-template>
</div>
</div>


@Component(
selector: 'jfg-star-rating',
templateUrl: './star-rating.component.html',
styleUrls: ['./star-rating.component.scss'],
providers: [

provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => StarRatingComponent),
multi: true

]

)
export class StarRatingComponent implements ControlValueAccessor

stars: boolean = Array(3).fill(true);

// Allow the input to be disabled, and when it is make it somewhat transparent.
@Input() disabled = false;
@HostBinding('style.opacity')
get opacity()
return this.disabled ? 1 : 1;


// Function to call when the rating changes.
onChange = (rating: number) =>
;

// Function to call when the input is touched (when a star is clicked).
onTouched = () =>
;


get value(): number
if(!this.disabled)
return this.stars.reduce((total, starred) =>
return total + (starred ? 1 : 0);
, 0);


rate(rating: number)
if (!this.disabled)
this.writeValue(rating);



// Allows Angular to update the model (rating).
// Update the model and changes needed for the view here.
writeValue(rating: number): void
if (!this.disabled)
this.stars = this.stars.map((_, i) => rating > i);
this.onChange(this.value);




// Allows Angular to register a function to call when the model (rating) changes.
// Save the function as a property to call later here.
registerOnChange(fn: (rating: number) => void): void
this.onChange = fn;


// Allows Angular to register a function to call when the input has been touched.
// Save the function as a property to call later here.
registerOnTouched(fn: () => void): void
this.onTouched = fn;


// Allows Angular to disable the input.
setDisabledState(isDisabled: boolean): void
this.disabled = isDisabled;






Im trying to make a component for star rating. I made it to work properly as an input. So i can press on the stars to get the correct value as a formControl and pass it to my services. But my problem is that i have tried to make my component to get value as an @input and set number of stars based on that value. I have tried inputing the value and setting it everywhere i could but still had no effect. If you could suggest me how can i proceed to set value form the input i would be glad :)










share|improve this question























  • The only input in that is the disabled property?
    – user184994
    Nov 11 at 11:15










  • Beforeu, is a custom form control component. So, the logic is that work as any input, you can use < star-ratting [(ngModel)]="your_variable" /> or < star-ratting fromControlName="myControl" />
    – Eliseo
    Nov 11 at 11:23












up vote
1
down vote

favorite









up vote
1
down vote

favorite











<div class="rating">
<div style="display: inline-block"
*ngFor="let starred of stars; let i = index"
(click)="rate(i + (starred ? (value > i + 1 ? 1 : 0) : 1))">
<ng-container *ngIf="starred; else noStar"><mat-icon class="filled">star</mat-icon></ng-container>
<ng-template #noStar><mat-icon class="empty">star_outline</mat-icon></ng-template>
</div>
</div>


@Component(
selector: 'jfg-star-rating',
templateUrl: './star-rating.component.html',
styleUrls: ['./star-rating.component.scss'],
providers: [

provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => StarRatingComponent),
multi: true

]

)
export class StarRatingComponent implements ControlValueAccessor

stars: boolean = Array(3).fill(true);

// Allow the input to be disabled, and when it is make it somewhat transparent.
@Input() disabled = false;
@HostBinding('style.opacity')
get opacity()
return this.disabled ? 1 : 1;


// Function to call when the rating changes.
onChange = (rating: number) =>
;

// Function to call when the input is touched (when a star is clicked).
onTouched = () =>
;


get value(): number
if(!this.disabled)
return this.stars.reduce((total, starred) =>
return total + (starred ? 1 : 0);
, 0);


rate(rating: number)
if (!this.disabled)
this.writeValue(rating);



// Allows Angular to update the model (rating).
// Update the model and changes needed for the view here.
writeValue(rating: number): void
if (!this.disabled)
this.stars = this.stars.map((_, i) => rating > i);
this.onChange(this.value);




// Allows Angular to register a function to call when the model (rating) changes.
// Save the function as a property to call later here.
registerOnChange(fn: (rating: number) => void): void
this.onChange = fn;


// Allows Angular to register a function to call when the input has been touched.
// Save the function as a property to call later here.
registerOnTouched(fn: () => void): void
this.onTouched = fn;


// Allows Angular to disable the input.
setDisabledState(isDisabled: boolean): void
this.disabled = isDisabled;






Im trying to make a component for star rating. I made it to work properly as an input. So i can press on the stars to get the correct value as a formControl and pass it to my services. But my problem is that i have tried to make my component to get value as an @input and set number of stars based on that value. I have tried inputing the value and setting it everywhere i could but still had no effect. If you could suggest me how can i proceed to set value form the input i would be glad :)










share|improve this question















<div class="rating">
<div style="display: inline-block"
*ngFor="let starred of stars; let i = index"
(click)="rate(i + (starred ? (value > i + 1 ? 1 : 0) : 1))">
<ng-container *ngIf="starred; else noStar"><mat-icon class="filled">star</mat-icon></ng-container>
<ng-template #noStar><mat-icon class="empty">star_outline</mat-icon></ng-template>
</div>
</div>


@Component(
selector: 'jfg-star-rating',
templateUrl: './star-rating.component.html',
styleUrls: ['./star-rating.component.scss'],
providers: [

provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => StarRatingComponent),
multi: true

]

)
export class StarRatingComponent implements ControlValueAccessor

stars: boolean = Array(3).fill(true);

// Allow the input to be disabled, and when it is make it somewhat transparent.
@Input() disabled = false;
@HostBinding('style.opacity')
get opacity()
return this.disabled ? 1 : 1;


// Function to call when the rating changes.
onChange = (rating: number) =>
;

// Function to call when the input is touched (when a star is clicked).
onTouched = () =>
;


get value(): number
if(!this.disabled)
return this.stars.reduce((total, starred) =>
return total + (starred ? 1 : 0);
, 0);


rate(rating: number)
if (!this.disabled)
this.writeValue(rating);



// Allows Angular to update the model (rating).
// Update the model and changes needed for the view here.
writeValue(rating: number): void
if (!this.disabled)
this.stars = this.stars.map((_, i) => rating > i);
this.onChange(this.value);




// Allows Angular to register a function to call when the model (rating) changes.
// Save the function as a property to call later here.
registerOnChange(fn: (rating: number) => void): void
this.onChange = fn;


// Allows Angular to register a function to call when the input has been touched.
// Save the function as a property to call later here.
registerOnTouched(fn: () => void): void
this.onTouched = fn;


// Allows Angular to disable the input.
setDisabledState(isDisabled: boolean): void
this.disabled = isDisabled;






Im trying to make a component for star rating. I made it to work properly as an input. So i can press on the stars to get the correct value as a formControl and pass it to my services. But my problem is that i have tried to make my component to get value as an @input and set number of stars based on that value. I have tried inputing the value and setting it everywhere i could but still had no effect. If you could suggest me how can i proceed to set value form the input i would be glad :)







angular typescript angular-material






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 9:55









Eluvatar

2,0291421




2,0291421










asked Nov 11 at 11:11









Beforeu

154




154











  • The only input in that is the disabled property?
    – user184994
    Nov 11 at 11:15










  • Beforeu, is a custom form control component. So, the logic is that work as any input, you can use < star-ratting [(ngModel)]="your_variable" /> or < star-ratting fromControlName="myControl" />
    – Eliseo
    Nov 11 at 11:23
















  • The only input in that is the disabled property?
    – user184994
    Nov 11 at 11:15










  • Beforeu, is a custom form control component. So, the logic is that work as any input, you can use < star-ratting [(ngModel)]="your_variable" /> or < star-ratting fromControlName="myControl" />
    – Eliseo
    Nov 11 at 11:23















The only input in that is the disabled property?
– user184994
Nov 11 at 11:15




The only input in that is the disabled property?
– user184994
Nov 11 at 11:15












Beforeu, is a custom form control component. So, the logic is that work as any input, you can use < star-ratting [(ngModel)]="your_variable" /> or < star-ratting fromControlName="myControl" />
– Eliseo
Nov 11 at 11:23




Beforeu, is a custom form control component. So, the logic is that work as any input, you can use < star-ratting [(ngModel)]="your_variable" /> or < star-ratting fromControlName="myControl" />
– Eliseo
Nov 11 at 11:23












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You already have implemented ControlValueAccessor so you should be able to set the value by ngModel which is two binding. You don't need any other input to set the value. So you can use your StarRatingComponent as -



<jfg-star-rating [ngModel]="3"></jfg-star-rating>


OR



<jfg-star-rating [(ngModel)]="rating"></jfg-star-rating>


Working copy is here - https://stackblitz.com/edit/angular-material-sample-vv4s6b






share|improve this answer




















  • yep that works perectly. Thank you :)
    – Beforeu
    Nov 11 at 11:57










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248133%2fangular-custom-control-star-rating-value-from-input%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










You already have implemented ControlValueAccessor so you should be able to set the value by ngModel which is two binding. You don't need any other input to set the value. So you can use your StarRatingComponent as -



<jfg-star-rating [ngModel]="3"></jfg-star-rating>


OR



<jfg-star-rating [(ngModel)]="rating"></jfg-star-rating>


Working copy is here - https://stackblitz.com/edit/angular-material-sample-vv4s6b






share|improve this answer




















  • yep that works perectly. Thank you :)
    – Beforeu
    Nov 11 at 11:57














up vote
2
down vote



accepted










You already have implemented ControlValueAccessor so you should be able to set the value by ngModel which is two binding. You don't need any other input to set the value. So you can use your StarRatingComponent as -



<jfg-star-rating [ngModel]="3"></jfg-star-rating>


OR



<jfg-star-rating [(ngModel)]="rating"></jfg-star-rating>


Working copy is here - https://stackblitz.com/edit/angular-material-sample-vv4s6b






share|improve this answer




















  • yep that works perectly. Thank you :)
    – Beforeu
    Nov 11 at 11:57












up vote
2
down vote



accepted







up vote
2
down vote



accepted






You already have implemented ControlValueAccessor so you should be able to set the value by ngModel which is two binding. You don't need any other input to set the value. So you can use your StarRatingComponent as -



<jfg-star-rating [ngModel]="3"></jfg-star-rating>


OR



<jfg-star-rating [(ngModel)]="rating"></jfg-star-rating>


Working copy is here - https://stackblitz.com/edit/angular-material-sample-vv4s6b






share|improve this answer












You already have implemented ControlValueAccessor so you should be able to set the value by ngModel which is two binding. You don't need any other input to set the value. So you can use your StarRatingComponent as -



<jfg-star-rating [ngModel]="3"></jfg-star-rating>


OR



<jfg-star-rating [(ngModel)]="rating"></jfg-star-rating>


Working copy is here - https://stackblitz.com/edit/angular-material-sample-vv4s6b







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 11:45









Sunil Singh

5,6711625




5,6711625











  • yep that works perectly. Thank you :)
    – Beforeu
    Nov 11 at 11:57
















  • yep that works perectly. Thank you :)
    – Beforeu
    Nov 11 at 11:57















yep that works perectly. Thank you :)
– Beforeu
Nov 11 at 11:57




yep that works perectly. Thank you :)
– Beforeu
Nov 11 at 11:57

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248133%2fangular-custom-control-star-rating-value-from-input%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線