I think you are thinking of this in a "jquery way". If you want operations on the image in your component you need to add []
to the attribute that you want to effect.
For example, because you want a dynamic image you need to apply property bindings ,which you can use in you component as a method or a variable. So you can add functionality on the image tag with a method on the src
attribute directly
<img [src]="image()" />
And in the component you add
image(){
//
}
Or assign it, so that when the action occurs that assigns the image to the image tag then assign it to a variable
<img [src]="imageScr" />
<button ion-button (click)="onImageUpload()"> Add </button>
Component
imageSrc:string = 'http://placehold.it/350x150';
constructor(...){}
onImageUpload(){
let uploadedImage = // get image stuff here
this.imageScr = uploadedImage;
}
Without going too much off topic I would suggest that you create an angular reactive form to your input types. This creates a better structure with baked in functionality that helps with most functionality that you need. So instead of a property binding you add a formControlName
with your formGroup
holding all the form input values.
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
export class ImagePage{
imageUploadForm: FormGroup;
imagePattern:string = '(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\.(?:jpg|gif|png))(?:\?([^#]*))?(?:#(.*))?'
constructor(public navCtrl: NavController, public navParams: NavParams,
public formBuilder: FormBuilder ){
this.imageUploadForm = this.formBuilder.group({
image:['http://placehold.it/350x150',[Validators.required,Validators.pattern(this.imagePattern)]]
imageName:[''[Validators.required]]
})
}
}
And then in your Html
<form [formGroup]="imageUploadForm" novalidate>
<ion-item>
<ion-img width="80" height="80" [src]="imageUploadForm.get('image').value"></ion-img>
<input type="file" name="pic" accept="image/*" formControlName="image">
</ion-item>
<ion-item class="item-error" >
<ion-label *ngIf="imageUploadForm.get('image').errors?.required"> Image is required </ion-label>
<ion-label *ngIf="imageUploadForm.get('image').errors?.pattern"> Image file must end in .jpg, .png or gif </ion-label>
</ion-item>
...
So now all the functionality that you want is controlled in the reactive form as a formControl
in a formGroup
which i think allows for a much greater flexibility as well as a better defined group of code
You can view the official documentation on reactive forms here