Get input datas from html to .ts


#1

Hi,
I have a little problem with my code,
i have to save datas written by the user but i can’t get it.

In this kind of input:

        <ion-item>
          <ion-label stacked>First Name</ion-label>
          <ion-input type="text" aria-placeholder="First Name" placeholder="First Name" [(ngModel)]="firstName" name="firstName"></ion-input>
        </ion-item>

And my .ts:

firstName = ' ';
public qObj = {
  firstName: this.firstName,
};

When the user press the button “SAVE” the variable into the qObj is ’ ’ not the name he wrote…

I don’t know how should i do…

Thanks for your time !


#2

try this one
[(ngModel)]=“qObj.firstName”


#3

I got an error :
ERROR TypeError: Cannot read property ‘firstName’ of undefined
at Object.eval [as updateDirectives]


#4

One of my general rules is “don’t put the same thing in multiple places”, because doing so leads to bugs like this when they get out of sync. So I would recommend losing either firstName or qObj as an object property.


#5

Do you mean that i have to delete the "public firstName = ’ '; " ?


#6

That’s one option. Another would be making qObj lexically scoped, assuming it needs to exist at all. Just don’t have them both be in the same scope (as object properties of the component).


#7

The fact is i’m a beginner in Ionic3 and scopes are a bit strange for me. I don’t even know where put things like:
app.controller(‘myCtrl’, function()) etc…


#8

That would be AngularJS (i.e. Angular 1) syntax, and thus won’t be used in Ionic 3.

Scope in this case refers to JavaScript/TypeScript scope, and not the AngularJS concept of $scope.