0

I'm using bootstrap 4 with Angular 2 and have the following select:

<div class="form-group">
    <label for="isActived" class="col-sm-4 control-label">Add BY</label>
    <div class="col-sm-16">
        <ng-container *ngFor="let user of users">
            <select class="custom-select" *ngIf="user.name == test" name="State" required
                [(ngModel)]="model.isActived" #isActived="ngModel">
                <option selected value="0">{{test}}</option>
            </select>
        </ng-container>
    </div>
</div>

My first question is when i use this selected i can't get it as default

my second one is how i can convert the value of test to integer i get this error: "message": "SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: i use the parseInt() and many solution but i still have the same error

getAllUsers() {
    this.userService
        .showAllUsers()
        .subscribe(users => {
            this.users = users;
            let userData = localStorage.getItem('currentUser');

            if (!userData) return null;

            let userrData = JSON.parse(userData);
            var jwt = require('jsonwebtoken');
            const tokenPayload = decode(userrData.token, {complete: true});

            for (let user of users) {
                if (tokenPayload.sub == user.id) {
                    parseInt(this.test = user.name);
                }
            }
        });
}

so can i get some help

7
  • If user.name is a string but you want it to be a number, you can use the syntax this.test = +user.name to convert it to a number. If you want to use parseInt(..), you need to swap your code to this.test = parseInt(user.name, 10) Commented Apr 25, 2018 at 23:07
  • thank you, but Now the test give me NAN Commented Apr 25, 2018 at 23:18
  • That would be because user.name is not a number. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Apr 25, 2018 at 23:19
  • yes it's string Commented Apr 25, 2018 at 23:22
  • 1
    The contents of the string are not a number. If it was a number it would return a number after parsing. Are you sure user.name is the property that you want to convert to a number? Commented Apr 25, 2018 at 23:23

1 Answer 1

1

I use this function (Number) to convert a string to a number.

Example:

let value = Number(item.someString);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.