epetre
April 27, 2016, 2:27am
1
First of all I am new to Ionic2 and even Ionic and Angular in general.
I am trying to implement slides and I need to have a reference to the slider so I can trigger the next event with a custom button.
To do this, it is suggested to get a reference like so (In TypeScript):
@ViewChild('slides_element_id') slider;
Since I am using ES6, it gives me:
Missing class properties transform
And this @ViewChild('onboarding_slides') slider: any;
Gives Unexpected token
for the :
I see alot of people saying that this is not the way to do it in ES6 as it is not supported, and I donât really mind because Iâm not a fan of strong tying. But I see no examples on how to properly get a reference to the silder.
This didnât work.
Alexd
April 27, 2016, 3:45am
2
1 Like
epetre
April 27, 2016, 4:59am
3
Thank you for the quick reply.
I will try again tomorrow, I must be tired, this look more complicated than I expected. I thought it would be easy to get a reference to an element in the page.
epetre
April 27, 2016, 2:31pm
4
Ok, that works.
So this TypeScript way to access a view child:
@ViewChild('mySlider') slider: Slides;
Is done by adding queries to the Component in ES6:
queries: {
slider: new ViewChild('mySlider')
}
1 Like
Pulling from the site docs
In TypeScript we can also use the queries metadata instead of the @ViewChild and @ContentChild property decorators.
So this works for both @ViewChild and @ContentChild
1 Like
Can you give more details about it? Iâm trying to do something similar without any successâŚ
Here is an example.
import {ViewChild} from 'angular2/core';
and on your Page decorators,
@Page({
templateUrl: 'build/pages/slides/slides.html',
queries: {
slider: new ViewChild('my_slides')
}
})
this.slider
can now be called.
1 Like
Hi @epetre , didnt work for me tooâŚ
@Page({
templateUrl: 'build/pages/slides/slides.html',
queries: {
slider: new ViewChild('my_slides')
}
})
How can i access âqueriesâ => âsliderâ key?
constructor(app, nav, slider) {
this.slider = slider;
}
Thanks!
Donât do it in the constructor, itâs already assigned to this.slider by that time and you are probably overriding it with an empty object.
1 Like
This works for meâŚ
JS
@Page({ templateUrl: 'path/to/myfile/.html', queries: { slider: new ViewChild('slides') } })
HTML
<ion-slides pager #slides> ... </ion-slides>
1 Like
epetre:
First of all I am new to Ionic2 and even Ionic and Angular in general.
I am trying to implement slides and I need to have a reference to the slider so I can trigger the next event with a custom button.
To do this, it is suggested to get a reference like so (In TypeScript): @ViewChild (âslides_element_idâ) slider;
Since I am using ES6, it gives me:Missing class properties transform
And this @ViewChild (âonboarding_slidesâ) slider: any; Gives Unexpected token for the :
I see alot of people saying that this is not the way to do it in ES6 as it is not supported, and I donât really mind because Iâm not a fan of strong tying. But I see no examples on how to properly get a reference to the silder.
try this
import { Slides } from âionic-angularâ;
and
@ViewChild (Slides) slides: Slides;
Jun711
May 16, 2017, 5:14pm
13
bsilva0x87:
JS
@Page ({
templateUrl: âpath/to/myfile/.htmlâ,
queries: {
slider: new ViewChild(âslidesâ)
}
})
HTML
<ion-slides pager #slides> ⌠</ion-slides>
It seems that you need to add a variable with the same name in the class to access the slider, right?