0

I need to combine the results of the select / option in a variable (it belongs to the URL of an API) and I am new handling Typescrip, then I get several errors and other things that I don't know how to do. For example, add the results of select / option (url next to cat, diff, type) and then save it in a localStorage to call it to another component (as it did in javascript)

html

<h5>Select Type:</h5>
            <form name="formul2" class="cont">
              <div class="box">
                <select id="type" name="type" [(ngModel)]="typ">
                  <option value="">Any Type</option>
                  <option value="&type=multiple">Multple Choice</option>
                  <option value="&type=boolean">True / False</option>
                </select>
              </div>
            </form>
            <a class="btn" id="fetch" href="game">Play</a>
            <a class="btn" href="highscores">High Scores</a>

TS

export class IndexComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

  public diff:string;

  public cat:string;

  public typ:string;
}

const url:string = 'https://opentdb.com/api.php?amount=20';

var newUrl:string = url + diff + cat + typ;

var x = document.getElementById('fetch')

x.addEventListener('click', function(e) {
  localStorage.setItem("urlGame", newUrl);
});
1
  • 1. url, newUrl , x can't just dangle around somewhere - you need to place them within your class. 2. x.addEventListener(...) needs to be wrapped in a method. 3. Read up about forms in angular, thereby on submit of the form you can retrieve the desired values of the form elements and process them according to your requirement. Commented Dec 5, 2019 at 6:42

3 Answers 3

1

Add a change listener method on the select element

<select id="type" name="type" [(ngModel)]="typ" (chnage)="setChange(typ)">
  <option value="">Any Type</option>
  <option value="&type=multiple">Multple Choice</option>
  <option value="&type=boolean">True / False</option>
</select>

Component

const url: string = 'https://opentdb.com/api.php?amount=20';
export class IndexComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

  public diff: string;

  public cat: string;

  public typ: string;

  public newUrl: string;

  public setChange(selectedValue: any) {
    this.newUrl = url + this.diff + this.cat + selectedValue;
    localStorage.setItem('urlGame', this.newUrl);
  }
}

You can use "ngx-webstorage-service" for local storage

Example

Sign up to request clarification or add additional context in comments.

2 Comments

I have three select elements, do I put the setChange () method on all three? (setChange (typ), setChange (diff), setChange (cat))
Basically if you bind seperate ngModel with every element, those variables will automatically gets the updated value. Not necessarily required to listen for all the change event
0

I guess what you are trying to achieve is this as you final url:

https://opentdb.com/api.php?amount=20/diff/cat/typ

If this is the case then it can be achieved using string template as follows:

var newUrl:string = `${this.url}/${this.diff}/${this.cat}/${this.typ}`

Comments

0

If you don't know about the data type then you can simple initialize as any

  public any_variable: any;

Try this :

let url:string = "https://www.example.com";
let any_variable:string = `${url}/api/${diff}`;

1 Comment

but those variable are string

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.