I try to dynamic create variables in class to store values and use it in ngModel and other placese.
I know, that I can assign value to variables in ngOnInit() like this
export class Component implements OnInit{
name: string;
ngOnInit(){
this.name = 'John Doe';
}
}
But I have some problem - I get my fields from server API and don't know what and how many items I get. I can only parsing server response and assign value to new variables after get it.
I can't do it like this (TS2540: Cannot assign to 'name' because it is a constant or a read-only property.)
export class Component implements OnInit{
ngOnInit(){
name = 'John Doe';
}
}
How can I assign new fields to my class in ngOnInit() or maybe in other place? (I think I can do it in constructor, but documentation say i shouldn't use it with Observable call to API and other difficult things)
You can use something like this to accomplish that:
ngOnInit() {
this['prop'] = 'value';
}
Here is a link to a working example: https://stackblitz.com/edit/dynamic-class-props
You can add an indexer to the class to be able to use any property name and not get a compiler error:
export class Component {
[name: string]: any;
ngOnInit(){
this["name"] = 'John Doe';
this.nameaa = "dd"
}
}
You should take care though, this means you can misspell property names and the compiler will not issue any error.
Related
In my angular application, i pass an object todo down to a child component via Input, which contains an array of strings at todo.tags. I now want to modify this array and therefore snap a copy of it and assign to a new variable like this:
#Input() todo: ITodos
public tags: Array<string> = [...this.todo.tags]
But i get the Error "property is used before initialization" which makes sense because i only declare todo . But i dont know how or for what i should initialize an Input, because the value is the input
The simple way is to init tags in getter for input like in this answer.
In case you are using RxJs in your project - you can pass an Observable, subscribe on it in the OnInit method and update tags on subscription firing.
#Input() todo: Observable<ITodos> = null;
public tags: Array<string> = null;
this.todo.pipe(untilDestroyed(this))
.subscribe(value => this.tags = [...value.tags]);
P.s. #ngneat/until-destroy is used for subscription clean up in the example
I have a ReactNative app and I am trying to write a test using Jest. My test need to use classes from a native component (react-native-nfc-manager) and a class I need is declared like this
export interface TagEvent {
ndefMessage: NdefRecord[];
maxSize?: number;
type?: string;
techTypes?: string[];
id?: number[];
}
and if I try to use this definition directly like
const event = new TagEvent();
I get this error
TypeError: _reactNativeNfcManager.default is not a constructor
I am coming from Java worlds so I think to myself - of course it cannot instantiate an interface, it needs a class so I write a test instance for this interface:
class TestTagEvent implements TagEvent {
ndefMessage: NdefRecord[] = []
maxSize?: number;
type?: string;
techTypes?: string[];
id?: number[];
}
But the problem seem to be different since it does not work either:
TypeError: _TestTagEvent.TestTagEvent is not a constructor
What am I missing?
You can't construct an object using an interface. An interface is just a set of properties that can be used to define required/available properties of variables.
To use the interface to initialize a variable, you want to do this:
const eventVar: TagEvent;
If you want to use TagEvent as a instantiable object, you need to define it as a class, not an interface. Something like this, where TagEventProps is the TagEvent interface you defined:
class TagEvent {
constructor(props: TagEventProps) {
this.ndefMesssage = props.nDefMessage;
//etc...
}
}
After further experiments problem has been solved by adding 'export' to the class. Admittedly, the original error description is absolutely not matching the root cause.
export class TestTagEvent implements TagEvent {
I recently started to learn how to use Mobx to manage my application's state and recently I came across the following error:
Object literal may only specify known properties, and "data" does not exist in type "AnnotatiosMap<this, never>".
This happens whenever I want to make a property of my class private. However, if it is public or protected, the problem does not occur.
This is a small snippet of my code:
import { makeObservable, observable } from "mobx";
class Base {
private data: string[];
constructor() {
this.data = [];
makeObservable(this, {
data: observable,
});
}
public getData = (): string[] => {
return this.data;
};
}
export default new Base();
What should I do to make my property private but still being watched?
Have a great day!
From the docs:
By default TypeScript will not allow you to annotate private fields. This can be overcome by explicitly passing the relevant private fields as generic argument, like this: makeObservable<MyStore, "privateField" | "privateField2">(this, { privateField: observable, privateField2: observable })
While reading about #Input() and #Output() I have found that we can use an alias instead of using the property name for the decorators.
Example
class ProductImage {
//Aliased
#Input('myProduct') product: string;
//Un-aliased
#Input() product: string;//not aliased
}
HTML
//Aliased attribute
<SomeComponent [myProduct] = "Product Name"></SomeComponent>
//Un-aliased attribute
<SomeComponent [product] = "Product Name"></SomeComponent>
The official Angular documentation says:
Sometimes we want the public name of an input/output property to be different from the internal name.
This is frequently the case with attribute directives. Directive consumers expect to bind to the name of the directive. For example, when we apply a directive with a myClick selector to a tag, we expect to bind to an event property that is also called myClick.
And This tutorial briefly explains it:
an alias let's me override the property name to be the alias instead of the original property name
Other than that I have not been able to find anything else on aliasing #Input() and #Output() on SO or through Google.
Things I would like to know are:
What does 'aliasing' attempt to achieve?
Is 'aliasing' something that we should be using regularly?
It's like your name and your nickname.
Inside your family you might be called Nic, where as , outside your family in order for other to know you legally , you should be called Nicolas.
So, if we consider the class of your component , the inside of your family :
#Component({
selector:'my-component'
})
export class MyComponent{
#Output('nicolas') nic = new EventEmitter<any>();
someFunction(){
// because we're inside the family ( inside this class ), instead of nicolas , we can call nic like this :
this.nic ...... ( which you'd normally emit an event ).
}
}
But from outside, you're still nicolas , so when someone want's to call you ( use that event emitter ) it should use nicolas;
<my-component (nicolas)="onNicolasChanged($event)"></my-component>
Note that we can't do this :
<my-component (nic)="onNicolasChanged($event)"></my-component>
This is aliasing that variable.
Also , please read my answer here :
What does 'aliasing' attempt to achieve?
it simply renames the input/output variable as per your needs. For example, if there is a hero object and when it is selected it is passed to another component. In that case it would be appropriate to call it selectedHero . Aliasing simply achieves that.
#Input('selectedHero') hero: string;
Is 'aliasing' something that we should be using regularly?
Depends on the context you are working on. Its not a necessity. For example it can be used to avoid conflicts in variable names , or for readability of the code.
Actually #Input is used when we are sending data of a parent component to child component and #Output is vice-versa. Example is
parent.component.html
<p> Child first name is {{childfirstname}}</p>
<input placeholder="First Name" type="text" [(ngModel)]="firstname">
<button mat-button (click)="send()">Click Parent</button>
<app-child
[firstname]="firstname"
(sendfirstname)="reciveFirstname($event)"
*ngIf="flage">
<app-child>
ParentComponent.ts
import { Component } from '#angular/core';
#Component({
selector:.......
)}
export class ParentComponent {
flage: boolean = false;
firstname: string;
childfirstname: string;
}
send() {
this.flage = true;
}
reciveFirstname(firstname) {
this.childfirstname = firstname;
}
Child.component.html
<p> Parent first name is {{firstname}} <br>
parent last name is {{lastname}} </p>
<input placeholder="First Name" type="text" [(ngModel)]="firstnamechild">
<button mat-button (click)="send">Send Parent</button>
child.component.ts
import { Component,Input, Output, EventEmitter } from '#angular/core';
#Component({
selector:'app-child',
........
})
export class ChildComponent {
firstnamechild: string;
#Input() firstname: string;
#Output()
sendfirstname: EventEmitter<string> = new EventEmitter<string>();
constructor() {}
send() {
this.sendfirstname.emit(this.firstnamechild);
}
}
A good reason to use it is if you ever need to change the variable name in the component, you don't need to change it in your template because it's using the alias. It's a simple way to prevent your code from breaking if another dev ever changes that variable name.
Following a post regarding creating objects dynamically in TypeScript, I have the following code used as a factory to create an object from its name:
public createComponent(context: Object, componentName: string): ComponentRef<ComponentBase> {
this.viewContainer.clear();
var instance =
Object.create(context[componentName].prototype); // <-- fails
let componentFactory =
this.componentFactoryResolver.resolveComponentFactory(instance);
return <ComponentRef<ComponentBase>> this.viewContainer.createComponent(componentFactory);
}
I'm not entirely convinced I understand this window[suchAndSuch] syntax: what does it mean? Can't find any documentation for it.
In any event it seems that window[myClassName] is undefined, even though the class in question is defined in the same file. I have looked at the Javascript transpiled from the TypeScript, and the class in question is in scope.
I think.
Help!
-- UPDATE --
I have this code, which is part of an Angular 2 application, that is calling the above method to try to get an instance of a component, injectables and all:
export class CustomUserComponent implements OnChanges {
#Input() componentType: string;
#ViewChild(ComponentDirective) componentAnchor: ComponentDirective;
ref: ComponentRef<GalleriaComponentBase>;
ngAfterViewInit() {
this.ref = this.componentAnchor
.createComponent(window, this.componentType);
}
}