How to extend a javascript object to include a method using _.extend or $.extend?
Date : March 29 2020, 07:55 AM
Any of those help For example using Underscore library, the following code works: , Remove the extra ); var myobject = {};
_.extend(myobject, {
method: function() {
return 'demo'
} // ); <-- remove this
});
|
Angular2 No provider for Renderer! (NgModel -> Token NgValueAccessor -> DefaultValueAccessor -> Renderer)
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I custom modal of customModal.ts in shlomiassaf/angular2-modal. Specific, i add a input contain ngModel, it imported FORM_DIRECTIVES and directives. , I see two possibilities: import {Component,Renderer,ElementRef} from 'angular2/core';
@Component({
selector: 'child',
template: '<div></div>',
})
export class ChildComponent {
constructor(private _renderer: Renderer,
private el: ElementRef) {
(...)
}
}
var otherResolved = Injector.resolve([
provide(DialogRef, { useValue: dialogRef})
provide(Renderer, { useValue: this.renderer})
]);
|
Implement DefaultValueAccessor won't trigger "registerOnChange"
Date : March 29 2020, 07:55 AM
Does that help You need to implement a custom ControlValueAccessor instead of extending an DefaultValueAccessor. https://github.com/angular/angular/issues/9146#issuecomment-241191899To implement ControlValueAccessor you should provide NG_VALUE_ACCESSOR token for this directive. The simplest solution could look like this: import {
Input, forwardRef, Renderer2,
Injector, ElementRef, Directive
} from '@angular/core'
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NgControl } from '@angular/forms';
export const PREV_VAL_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => PrevValDirective),
multi: true
};
@Directive({
selector: '[ngModel][prevVal]',
host: {
'(input)': 'handleInput($event.target.value)',
'(blur)': 'onTouched()',
},
providers: [PREV_VAL_VALUE_ACCESSOR]
})
export class PrevValDirective implements ControlValueAccessor {
@Input() prevVal: string;
onChange = (_: any) => {};
onTouched = () => {};
constructor(
private _renderer: Renderer2,
private _elementRef: ElementRef,
private _inj: Injector) {
}
writeValue(value: any): void {
const normalizedValue = value == null ? '' : value;
this._renderer.setProperty(this._elementRef.nativeElement, 'value', normalizedValue);
}
registerOnChange(fn: (_: any) => void): void {
this.onChange = fn;
}
registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
}
handleInput(value: any): void {
if (value === this.prevVal) {
let model = this._inj.get(NgControl); // get NgModel
model.control.setValue(model.value); // set previous value
return;
}
this.onChange(value);
}
}
let model = this._inj.get(NgControl);
<input type="text" [(ngModel)]="val" prevVal="false">
|
For custom form components, is it possible to use DefaultValueAccessor instead of ControlValueAccessor?
Date : March 29 2020, 07:55 AM
it should still fix some issue One possible solution could be using ngDefaultControl attribute on your custom component: <div [formGroup]="form">
<jg-search formControlName="x" ngDefaultControl></jg-search>
^^^^^^^^^^^^^^^^
</div>
@Component({
selector: 'jg-search',
template: `
<input [formControl]="ngControl.control">
`
})
export class MyInput {
constructor(public ngControl: NgControl) {}
}
|
TypeError: Class constructor DefaultValueAccessor cannot be invoked without 'new'
Date : March 29 2020, 07:55 AM
should help you out I've resolved by making the class implements ControlValueAccessor instead of extends DefaultValueAccessor. As a consequence of that, I also had to implement the missing methods, so I added the following: import { ControlValueAccessor } from "@angular/forms";
// ...
export class MyClass implements ControlValueAccessor {
// ...
onChange = (_: any) => {};
onTouched = () => {};
// ...
registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
setDisabledState(isDisabled: boolean): void {
this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
}
}
|