Nativescript Observable Something is not correct





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Have spent several hours just to get this simple piece to work. Complex arrays are working fine, but dont understand why this simple code does not render data. Using nativescript 5.0



.ts file



public _contact: ObservableArray<string>;

ngOnInit(): void {

}

public pickContact()
{
Permissions.requestPermissions([android.Manifest.permission.GET_ACCOUNTS, android.Manifest.permission.WRITE_CONTACTS, android.Manifest.permission.READ_PHONE_STATE], "Access to google account required!")
.then(() => {
contacts.getContact().then(function(args){
if (args.response === "selected") {
console.log("arg response selected .... ");
var _contact = args.data;
var name_given = _contact.name.given == null ? "" : _contact.name.given;
var name_middle = _contact.name.middle == null ? "" : _contact.name.middle;
var name_family = _contact.name.family == null ? "" : _contact.name.family;
var _contactName= name_given + " " + name_middle + " " + name_family;
var _contactPhone = _contact.phoneNumbers[0].value;
alert("Data = " + _contactName + " " + _contactPhone);
this._contact = new ObservableArray([
{name:_contactName, phone:_contactPhone}
]);

}
})
});
}


.html file



<StackLayout>
<Button width="100" height="100" row="0" col="0" text="Refresh Calls" (tap)="pickContact()" horizontalAlignment="left" class="btn btn-rounded-sm m-20" style="background-color: rgb(60, 72, 179);color:white;font-weight:bold;"></Button>
<ListView [items]="_contact" height = "270" class="list-group">
<ng-template let-result="item">
<GridLayout rows="*,*" cols="" horizontalAlignment="left" verticalAlignment="top">
<Label row="0" col="0" [text]="result.name" class="h2 list-group-item lblThin" style="font-size:22;" (tap)="dialNumber(result.mobile)"></Label>
<Label row="1" col="0" [text]="result.phone" class="h4 list-group-item lblThin" (tap)="dialNumber(result.mobile)"></Label>
</GridLayout>
</ng-template>
</ListView>
</StackLayout>


height in Listview is 270 (fixed value) as I expect only one record to be in this Listview. Contact selection will just pick one contact and its name & number will be displayed in the Listview. alert shows correct data, but no rendering of data in html.



In addition to above rendering issue, I want to know isthere any way to avoid button to call pickNumber function. I have tried navigatingTo, ngOnInit and noticed that they dont get executed.










share|improve this question

























  • I guess contact will be an ObservableArray, so may I ask why you are trying to access it's property phone - this.contact.phone=_contactPhone;. Do you have a sample repo?

    – Manoj
    Nov 23 '18 at 17:34











  • Below line was due to copy paste from other component.ts file, removed it and edited the post too this.contact.phone=_contactPhone;

    – Pratik
    Nov 23 '18 at 17:38











  • Does the alert show the right information?

    – Ian MacDonald
    Nov 23 '18 at 17:47













  • Yes Ian, it shows perfect data.

    – Pratik
    Nov 23 '18 at 17:49


















0















Have spent several hours just to get this simple piece to work. Complex arrays are working fine, but dont understand why this simple code does not render data. Using nativescript 5.0



.ts file



public _contact: ObservableArray<string>;

ngOnInit(): void {

}

public pickContact()
{
Permissions.requestPermissions([android.Manifest.permission.GET_ACCOUNTS, android.Manifest.permission.WRITE_CONTACTS, android.Manifest.permission.READ_PHONE_STATE], "Access to google account required!")
.then(() => {
contacts.getContact().then(function(args){
if (args.response === "selected") {
console.log("arg response selected .... ");
var _contact = args.data;
var name_given = _contact.name.given == null ? "" : _contact.name.given;
var name_middle = _contact.name.middle == null ? "" : _contact.name.middle;
var name_family = _contact.name.family == null ? "" : _contact.name.family;
var _contactName= name_given + " " + name_middle + " " + name_family;
var _contactPhone = _contact.phoneNumbers[0].value;
alert("Data = " + _contactName + " " + _contactPhone);
this._contact = new ObservableArray([
{name:_contactName, phone:_contactPhone}
]);

}
})
});
}


.html file



<StackLayout>
<Button width="100" height="100" row="0" col="0" text="Refresh Calls" (tap)="pickContact()" horizontalAlignment="left" class="btn btn-rounded-sm m-20" style="background-color: rgb(60, 72, 179);color:white;font-weight:bold;"></Button>
<ListView [items]="_contact" height = "270" class="list-group">
<ng-template let-result="item">
<GridLayout rows="*,*" cols="" horizontalAlignment="left" verticalAlignment="top">
<Label row="0" col="0" [text]="result.name" class="h2 list-group-item lblThin" style="font-size:22;" (tap)="dialNumber(result.mobile)"></Label>
<Label row="1" col="0" [text]="result.phone" class="h4 list-group-item lblThin" (tap)="dialNumber(result.mobile)"></Label>
</GridLayout>
</ng-template>
</ListView>
</StackLayout>


height in Listview is 270 (fixed value) as I expect only one record to be in this Listview. Contact selection will just pick one contact and its name & number will be displayed in the Listview. alert shows correct data, but no rendering of data in html.



In addition to above rendering issue, I want to know isthere any way to avoid button to call pickNumber function. I have tried navigatingTo, ngOnInit and noticed that they dont get executed.










share|improve this question

























  • I guess contact will be an ObservableArray, so may I ask why you are trying to access it's property phone - this.contact.phone=_contactPhone;. Do you have a sample repo?

    – Manoj
    Nov 23 '18 at 17:34











  • Below line was due to copy paste from other component.ts file, removed it and edited the post too this.contact.phone=_contactPhone;

    – Pratik
    Nov 23 '18 at 17:38











  • Does the alert show the right information?

    – Ian MacDonald
    Nov 23 '18 at 17:47













  • Yes Ian, it shows perfect data.

    – Pratik
    Nov 23 '18 at 17:49














0












0








0








Have spent several hours just to get this simple piece to work. Complex arrays are working fine, but dont understand why this simple code does not render data. Using nativescript 5.0



.ts file



public _contact: ObservableArray<string>;

ngOnInit(): void {

}

public pickContact()
{
Permissions.requestPermissions([android.Manifest.permission.GET_ACCOUNTS, android.Manifest.permission.WRITE_CONTACTS, android.Manifest.permission.READ_PHONE_STATE], "Access to google account required!")
.then(() => {
contacts.getContact().then(function(args){
if (args.response === "selected") {
console.log("arg response selected .... ");
var _contact = args.data;
var name_given = _contact.name.given == null ? "" : _contact.name.given;
var name_middle = _contact.name.middle == null ? "" : _contact.name.middle;
var name_family = _contact.name.family == null ? "" : _contact.name.family;
var _contactName= name_given + " " + name_middle + " " + name_family;
var _contactPhone = _contact.phoneNumbers[0].value;
alert("Data = " + _contactName + " " + _contactPhone);
this._contact = new ObservableArray([
{name:_contactName, phone:_contactPhone}
]);

}
})
});
}


.html file



<StackLayout>
<Button width="100" height="100" row="0" col="0" text="Refresh Calls" (tap)="pickContact()" horizontalAlignment="left" class="btn btn-rounded-sm m-20" style="background-color: rgb(60, 72, 179);color:white;font-weight:bold;"></Button>
<ListView [items]="_contact" height = "270" class="list-group">
<ng-template let-result="item">
<GridLayout rows="*,*" cols="" horizontalAlignment="left" verticalAlignment="top">
<Label row="0" col="0" [text]="result.name" class="h2 list-group-item lblThin" style="font-size:22;" (tap)="dialNumber(result.mobile)"></Label>
<Label row="1" col="0" [text]="result.phone" class="h4 list-group-item lblThin" (tap)="dialNumber(result.mobile)"></Label>
</GridLayout>
</ng-template>
</ListView>
</StackLayout>


height in Listview is 270 (fixed value) as I expect only one record to be in this Listview. Contact selection will just pick one contact and its name & number will be displayed in the Listview. alert shows correct data, but no rendering of data in html.



In addition to above rendering issue, I want to know isthere any way to avoid button to call pickNumber function. I have tried navigatingTo, ngOnInit and noticed that they dont get executed.










share|improve this question
















Have spent several hours just to get this simple piece to work. Complex arrays are working fine, but dont understand why this simple code does not render data. Using nativescript 5.0



.ts file



public _contact: ObservableArray<string>;

ngOnInit(): void {

}

public pickContact()
{
Permissions.requestPermissions([android.Manifest.permission.GET_ACCOUNTS, android.Manifest.permission.WRITE_CONTACTS, android.Manifest.permission.READ_PHONE_STATE], "Access to google account required!")
.then(() => {
contacts.getContact().then(function(args){
if (args.response === "selected") {
console.log("arg response selected .... ");
var _contact = args.data;
var name_given = _contact.name.given == null ? "" : _contact.name.given;
var name_middle = _contact.name.middle == null ? "" : _contact.name.middle;
var name_family = _contact.name.family == null ? "" : _contact.name.family;
var _contactName= name_given + " " + name_middle + " " + name_family;
var _contactPhone = _contact.phoneNumbers[0].value;
alert("Data = " + _contactName + " " + _contactPhone);
this._contact = new ObservableArray([
{name:_contactName, phone:_contactPhone}
]);

}
})
});
}


.html file



<StackLayout>
<Button width="100" height="100" row="0" col="0" text="Refresh Calls" (tap)="pickContact()" horizontalAlignment="left" class="btn btn-rounded-sm m-20" style="background-color: rgb(60, 72, 179);color:white;font-weight:bold;"></Button>
<ListView [items]="_contact" height = "270" class="list-group">
<ng-template let-result="item">
<GridLayout rows="*,*" cols="" horizontalAlignment="left" verticalAlignment="top">
<Label row="0" col="0" [text]="result.name" class="h2 list-group-item lblThin" style="font-size:22;" (tap)="dialNumber(result.mobile)"></Label>
<Label row="1" col="0" [text]="result.phone" class="h4 list-group-item lblThin" (tap)="dialNumber(result.mobile)"></Label>
</GridLayout>
</ng-template>
</ListView>
</StackLayout>


height in Listview is 270 (fixed value) as I expect only one record to be in this Listview. Contact selection will just pick one contact and its name & number will be displayed in the Listview. alert shows correct data, but no rendering of data in html.



In addition to above rendering issue, I want to know isthere any way to avoid button to call pickNumber function. I have tried navigatingTo, ngOnInit and noticed that they dont get executed.







angular typescript nativescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 17:33







Pratik

















asked Nov 23 '18 at 17:27









PratikPratik

185




185













  • I guess contact will be an ObservableArray, so may I ask why you are trying to access it's property phone - this.contact.phone=_contactPhone;. Do you have a sample repo?

    – Manoj
    Nov 23 '18 at 17:34











  • Below line was due to copy paste from other component.ts file, removed it and edited the post too this.contact.phone=_contactPhone;

    – Pratik
    Nov 23 '18 at 17:38











  • Does the alert show the right information?

    – Ian MacDonald
    Nov 23 '18 at 17:47













  • Yes Ian, it shows perfect data.

    – Pratik
    Nov 23 '18 at 17:49



















  • I guess contact will be an ObservableArray, so may I ask why you are trying to access it's property phone - this.contact.phone=_contactPhone;. Do you have a sample repo?

    – Manoj
    Nov 23 '18 at 17:34











  • Below line was due to copy paste from other component.ts file, removed it and edited the post too this.contact.phone=_contactPhone;

    – Pratik
    Nov 23 '18 at 17:38











  • Does the alert show the right information?

    – Ian MacDonald
    Nov 23 '18 at 17:47













  • Yes Ian, it shows perfect data.

    – Pratik
    Nov 23 '18 at 17:49

















I guess contact will be an ObservableArray, so may I ask why you are trying to access it's property phone - this.contact.phone=_contactPhone;. Do you have a sample repo?

– Manoj
Nov 23 '18 at 17:34





I guess contact will be an ObservableArray, so may I ask why you are trying to access it's property phone - this.contact.phone=_contactPhone;. Do you have a sample repo?

– Manoj
Nov 23 '18 at 17:34













Below line was due to copy paste from other component.ts file, removed it and edited the post too this.contact.phone=_contactPhone;

– Pratik
Nov 23 '18 at 17:38





Below line was due to copy paste from other component.ts file, removed it and edited the post too this.contact.phone=_contactPhone;

– Pratik
Nov 23 '18 at 17:38













Does the alert show the right information?

– Ian MacDonald
Nov 23 '18 at 17:47







Does the alert show the right information?

– Ian MacDonald
Nov 23 '18 at 17:47















Yes Ian, it shows perfect data.

– Pratik
Nov 23 '18 at 17:49





Yes Ian, it shows perfect data.

– Pratik
Nov 23 '18 at 17:49












1 Answer
1






active

oldest

votes


















1














There are a few things that are wrong about this code.



public _contact: ObservableArray<string>;

this._contact = new ObservableArray([
{name:_contactName, phone:_contactPhone}
]);
/* { name: string, phone: string } is not of type 'string' */


Additionally, API callbacks that update UI elements typically like to be told which NgZone to operate within.



Then, to make matters a little muddier, you're using a disconnected function which technically shouldn't be able to reference any of your this members.



Try this:



class Contact: {
constructor(public name: string, public phone: string) { }
}

@Component({ ... })
export class YourComponent {
public contact$: Subject<Contact | null>;
constructor(private zone: NgZone, ...) {
this.contact$ = new BehaviourSubject<Contact | null>(null);
}

public pickContact() {
Permissions. ...
.then(() => {
contacts.getContact().then((args) => {
...
this.zone.run(() => {
this.contact$.next(new Contact(contactName, contactPhone));
}
});
});
}
}


To improve this further, you'll probably want to use some WeakRefs in there to make sure you're not holding onto object references indefinitely (causing memory leaks) while waiting for API calls.






share|improve this answer
























  • Above posted code in my post doesnot show complete class. I am using exact same structure of the code which is working fine, the only difference is in this code I am getting just one record and in other working code, I am getting 30 records. There is something I am missing wrt arrays etc

    – Pratik
    Nov 23 '18 at 19:11






  • 1





    I have highlighted the issues in the code that you have provided. I will not guess at what you are doing wrong in code that you haven't provided. Sorry.

    – Ian MacDonald
    Nov 23 '18 at 19:40












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450835%2fnativescript-observable-something-is-not-correct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














There are a few things that are wrong about this code.



public _contact: ObservableArray<string>;

this._contact = new ObservableArray([
{name:_contactName, phone:_contactPhone}
]);
/* { name: string, phone: string } is not of type 'string' */


Additionally, API callbacks that update UI elements typically like to be told which NgZone to operate within.



Then, to make matters a little muddier, you're using a disconnected function which technically shouldn't be able to reference any of your this members.



Try this:



class Contact: {
constructor(public name: string, public phone: string) { }
}

@Component({ ... })
export class YourComponent {
public contact$: Subject<Contact | null>;
constructor(private zone: NgZone, ...) {
this.contact$ = new BehaviourSubject<Contact | null>(null);
}

public pickContact() {
Permissions. ...
.then(() => {
contacts.getContact().then((args) => {
...
this.zone.run(() => {
this.contact$.next(new Contact(contactName, contactPhone));
}
});
});
}
}


To improve this further, you'll probably want to use some WeakRefs in there to make sure you're not holding onto object references indefinitely (causing memory leaks) while waiting for API calls.






share|improve this answer
























  • Above posted code in my post doesnot show complete class. I am using exact same structure of the code which is working fine, the only difference is in this code I am getting just one record and in other working code, I am getting 30 records. There is something I am missing wrt arrays etc

    – Pratik
    Nov 23 '18 at 19:11






  • 1





    I have highlighted the issues in the code that you have provided. I will not guess at what you are doing wrong in code that you haven't provided. Sorry.

    – Ian MacDonald
    Nov 23 '18 at 19:40
















1














There are a few things that are wrong about this code.



public _contact: ObservableArray<string>;

this._contact = new ObservableArray([
{name:_contactName, phone:_contactPhone}
]);
/* { name: string, phone: string } is not of type 'string' */


Additionally, API callbacks that update UI elements typically like to be told which NgZone to operate within.



Then, to make matters a little muddier, you're using a disconnected function which technically shouldn't be able to reference any of your this members.



Try this:



class Contact: {
constructor(public name: string, public phone: string) { }
}

@Component({ ... })
export class YourComponent {
public contact$: Subject<Contact | null>;
constructor(private zone: NgZone, ...) {
this.contact$ = new BehaviourSubject<Contact | null>(null);
}

public pickContact() {
Permissions. ...
.then(() => {
contacts.getContact().then((args) => {
...
this.zone.run(() => {
this.contact$.next(new Contact(contactName, contactPhone));
}
});
});
}
}


To improve this further, you'll probably want to use some WeakRefs in there to make sure you're not holding onto object references indefinitely (causing memory leaks) while waiting for API calls.






share|improve this answer
























  • Above posted code in my post doesnot show complete class. I am using exact same structure of the code which is working fine, the only difference is in this code I am getting just one record and in other working code, I am getting 30 records. There is something I am missing wrt arrays etc

    – Pratik
    Nov 23 '18 at 19:11






  • 1





    I have highlighted the issues in the code that you have provided. I will not guess at what you are doing wrong in code that you haven't provided. Sorry.

    – Ian MacDonald
    Nov 23 '18 at 19:40














1












1








1







There are a few things that are wrong about this code.



public _contact: ObservableArray<string>;

this._contact = new ObservableArray([
{name:_contactName, phone:_contactPhone}
]);
/* { name: string, phone: string } is not of type 'string' */


Additionally, API callbacks that update UI elements typically like to be told which NgZone to operate within.



Then, to make matters a little muddier, you're using a disconnected function which technically shouldn't be able to reference any of your this members.



Try this:



class Contact: {
constructor(public name: string, public phone: string) { }
}

@Component({ ... })
export class YourComponent {
public contact$: Subject<Contact | null>;
constructor(private zone: NgZone, ...) {
this.contact$ = new BehaviourSubject<Contact | null>(null);
}

public pickContact() {
Permissions. ...
.then(() => {
contacts.getContact().then((args) => {
...
this.zone.run(() => {
this.contact$.next(new Contact(contactName, contactPhone));
}
});
});
}
}


To improve this further, you'll probably want to use some WeakRefs in there to make sure you're not holding onto object references indefinitely (causing memory leaks) while waiting for API calls.






share|improve this answer













There are a few things that are wrong about this code.



public _contact: ObservableArray<string>;

this._contact = new ObservableArray([
{name:_contactName, phone:_contactPhone}
]);
/* { name: string, phone: string } is not of type 'string' */


Additionally, API callbacks that update UI elements typically like to be told which NgZone to operate within.



Then, to make matters a little muddier, you're using a disconnected function which technically shouldn't be able to reference any of your this members.



Try this:



class Contact: {
constructor(public name: string, public phone: string) { }
}

@Component({ ... })
export class YourComponent {
public contact$: Subject<Contact | null>;
constructor(private zone: NgZone, ...) {
this.contact$ = new BehaviourSubject<Contact | null>(null);
}

public pickContact() {
Permissions. ...
.then(() => {
contacts.getContact().then((args) => {
...
this.zone.run(() => {
this.contact$.next(new Contact(contactName, contactPhone));
}
});
});
}
}


To improve this further, you'll probably want to use some WeakRefs in there to make sure you're not holding onto object references indefinitely (causing memory leaks) while waiting for API calls.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 17:55









Ian MacDonaldIan MacDonald

9,24711333




9,24711333













  • Above posted code in my post doesnot show complete class. I am using exact same structure of the code which is working fine, the only difference is in this code I am getting just one record and in other working code, I am getting 30 records. There is something I am missing wrt arrays etc

    – Pratik
    Nov 23 '18 at 19:11






  • 1





    I have highlighted the issues in the code that you have provided. I will not guess at what you are doing wrong in code that you haven't provided. Sorry.

    – Ian MacDonald
    Nov 23 '18 at 19:40



















  • Above posted code in my post doesnot show complete class. I am using exact same structure of the code which is working fine, the only difference is in this code I am getting just one record and in other working code, I am getting 30 records. There is something I am missing wrt arrays etc

    – Pratik
    Nov 23 '18 at 19:11






  • 1





    I have highlighted the issues in the code that you have provided. I will not guess at what you are doing wrong in code that you haven't provided. Sorry.

    – Ian MacDonald
    Nov 23 '18 at 19:40

















Above posted code in my post doesnot show complete class. I am using exact same structure of the code which is working fine, the only difference is in this code I am getting just one record and in other working code, I am getting 30 records. There is something I am missing wrt arrays etc

– Pratik
Nov 23 '18 at 19:11





Above posted code in my post doesnot show complete class. I am using exact same structure of the code which is working fine, the only difference is in this code I am getting just one record and in other working code, I am getting 30 records. There is something I am missing wrt arrays etc

– Pratik
Nov 23 '18 at 19:11




1




1





I have highlighted the issues in the code that you have provided. I will not guess at what you are doing wrong in code that you haven't provided. Sorry.

– Ian MacDonald
Nov 23 '18 at 19:40





I have highlighted the issues in the code that you have provided. I will not guess at what you are doing wrong in code that you haven't provided. Sorry.

– Ian MacDonald
Nov 23 '18 at 19:40




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450835%2fnativescript-observable-something-is-not-correct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini