admin管理员组文章数量:1391798
We are developing ponents and when using them, we would like to use the same mechanism like for DOM nodes to conditionally define attributes. So for preventing attributes to show up at all, we set the value to null and its not existing in the final HTML output. Great!
<button [attr.disabled]="condition ? true : null"></button>
Now, when using our own ponents, this does not work. When we set null
, we actually get null
in the ponents @Input as the value. Any by default set value will be overwritten.
...
@Component({
selector: 'myElement',
templateUrl: './my-elementponent.html'
})
export class MyElementComponent {
@Input() type: string = 'default';
...
<myElment [type]="condition ? 'something' : null"></myElement>
So, whenever we read the type
in the ponent, we get null
instead of the 'default'
value which was set.
I tried to find a way to get the original default value, but did not find it. It is existing in the ngBaseDef
when accessed in constructor
time, but this is not working in production. I expected ngOnChanges
to give me the real (default) value in the first change that is done and therefore be able to prevent that null
is set, but the previousValue
is undefined
.
We came up with some ways to solve this:
- defining a
default
object and setting for every input the default value when itsnull
- addressing the DOM element in the template again, instead of setting null
<myElement #myelem [type]="condition ? 'something' : myelem.type"></myElement>
- defining set / get for every input to prevent null setting
_type: string = 'default';
@Input()
set type(v: string) {if (v !== null) this._type = v;}
get type() { return this._type; }
but are curious, if there are maybe others who have similar issues and how it got fixed. Also I would appreciate any other idea which is maybe more elegant.
Thanks!
We are developing ponents and when using them, we would like to use the same mechanism like for DOM nodes to conditionally define attributes. So for preventing attributes to show up at all, we set the value to null and its not existing in the final HTML output. Great!
<button [attr.disabled]="condition ? true : null"></button>
Now, when using our own ponents, this does not work. When we set null
, we actually get null
in the ponents @Input as the value. Any by default set value will be overwritten.
...
@Component({
selector: 'myElement',
templateUrl: './my-element.ponent.html'
})
export class MyElementComponent {
@Input() type: string = 'default';
...
<myElment [type]="condition ? 'something' : null"></myElement>
So, whenever we read the type
in the ponent, we get null
instead of the 'default'
value which was set.
I tried to find a way to get the original default value, but did not find it. It is existing in the ngBaseDef
when accessed in constructor
time, but this is not working in production. I expected ngOnChanges
to give me the real (default) value in the first change that is done and therefore be able to prevent that null
is set, but the previousValue
is undefined
.
We came up with some ways to solve this:
- defining a
default
object and setting for every input the default value when itsnull
- addressing the DOM element in the template again, instead of setting null
<myElement #myelem [type]="condition ? 'something' : myelem.type"></myElement>
- defining set / get for every input to prevent null setting
_type: string = 'default';
@Input()
set type(v: string) {if (v !== null) this._type = v;}
get type() { return this._type; }
but are curious, if there are maybe others who have similar issues and how it got fixed. Also I would appreciate any other idea which is maybe more elegant.
Thanks!
Share Improve this question asked Nov 4, 2019 at 15:00 FabianFabian 3013 silver badges7 bronze badges 1- possible duplicate of stackoverflow./questions/35839120/… and stackoverflow./questions/36071942/… – Naga Sai A Commented Nov 4, 2019 at 15:36
3 Answers
Reset to default 4There is no standard angular way, because many times you would want null
or undefined
as value. Your ideas are not bad solutions. I got a couple more
- I suppose you can also use the
ngOnChanges
hook for this:
@Input()
type: string = 'defaultType';
ngOnChanges(changes: SimpleChanges): void {
// == null to also match undefined
if (this.type == null) {
this.type = 'defaultType';
}
}
- Or using
Observables
:
private readonly _type$ = new BehaviorSubject('defaultType');
readonly type$ = this._type$.pipe(
map((type) => type == null ? 'defaultType' : type)
);
@Input()
set type(type: string) {
this._type$.next(type);
}
- Or create your own decorator playground
function Default(value: any) {
return function(target: any, key: string | symbol) {
const valueAccessor = '__' + key.toString() + '__';
Object.defineProperty(target, key, {
get: function () {
return this[valueAccessor] != null ? this[valueAccessor] : value
},
set: function (next) {
if (!Object.prototype.hasOwnProperty.call(this, valueAccessor)) {
Object.defineProperty(this, valueAccessor, {
writable: true,
enumerable: false
});
}
this[valueAccessor] = next;
},
enumerable: true
});
};
}
which you can use like this:
@Input()
@Default('defaultType')
type!: string;
Just one more option (perhaps simpler if you don't want to implement your own custom @annotation) based off Poul Krujit solution:
const DEFAULT_VALUE = 'default';
export class MyElementComponent {
typeWrapped = DEFAULT_VALUE;
@Input()
set type(selected: string) {
// this makes sure only truthy values get assigned
// so [type]="null" or [type]="undefined" still go to the default.
if (selected) {
this.typeWrapped = selected;
} else {
this.typeWrapped = DEFAULT_VALUE;
}
}
get type() {
return this.typeWrapped;
}
}
If you need to do this for multiple inputs, you can also use a custom pipe
instead of manually defining the getter/setter and default for each input. The pipe
can contain the logic and defaultArg to return the defaultArg if the input is null
.
i.e.
// pipe
@Pipe({name: 'ifNotNullElse'})
export class IfNotNullElsePipe implements PipeTransform {
transform(value: string, defaultVal?: string): string {
return value !== null ? value : defaultVal;
}
}
<!-- myElem template -->
<p>Type Input: {{ type | ifNotNullElse: 'default' }}</p>
<p>Another Input: {{ anotherType | ifNotNullElse: 'anotherDefault' }}</p>
本文标签: javascriptAngular How to get default Input valueStack Overflow
版权声明:本文标题:javascript - Angular: How to get default @Input value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744769251a2624234.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论