Home Ask Login Register

Developers Planet

Your answer is one click away!

Alcadur February 2016

Pass arguments to custom component

I created component and it will be nice to use it ;) but I don't know how to pass arguments;


<Page xmlns="http://www.nativescript.org/tns.xsd"
    <masterMenu:masterMenu selected="1"/>

How can I catch selected in component? Currently I have only masterMenu.xml and masterManu.css files.

[ EDIT ]

Let say I have component named question (pseudo code):


< StackLayout loaded="loaded">

< Label text="Question: loremipsum" />
< Label text="Answer 1" id="answer1" />
< Label text="Answer 2" id="answer2"/>
< Label text="Answer 3" id="answer3"/>

< /StackLayout>


exports.loaded = function(args) {

var answerNo = args.answerNo,
    page = args.object;

page.getElementById(page, "answer"+answerNo).addClass('correct') 


used :

< Page xmlns="http://www.nativescript.org/tns.xsd" xmlns:question="includes/customComponents/question">

< question:question answerNo="2" />

< question:question answerNo="1" />
< question:question answerNo="3" />

< /Page>


Brad Martin February 2016

In your javascript you would access it like this:

var menu = page.getViewById("idOfMenuComponent");

That is assuming it's available to be accessed. That would all depend on how you have this component created. If it's not available you need to add the property to your component. Have a look at the <Button> module to see how this can be done.

Post Status

Asked in February 2016
Viewed 1,929 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life