2

I created a method that appends a string if a checkbox is checked, and if not checked, it should remove the value from the string using the replace() method

The code for the method:

current_search : string = "";
generateLink(e, n) {
    if(e.checked){
        this.current_search = this.current_search + " " + n + ",";
        console.log(this.current_search);
      } else {
        this.current_search.replace(n, '');
        console.log(this.current_search);
      }
}

Nothing gets updated on the string when the checkbox is unchecked. When it is checked, the string is appended with whatever 'n' is as that is being passed into the method.

2 Answers 2

4

actually replace methods returns the new string with some or all matches of a pattern replaced by a replacement, what your doing is not saving the return value of replace method

let a = "how ur doin";
a = a.replace('ur','');
console.log(a);

try by saving return value of replace method by
this.current_search = this.current_search.replace(n, '')

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

1 Comment

thumbs up. but doesnt it clash with concept of two way data binding or the replace function creates a new object.?
1

use bellow for angular 2+

_var.split("/").join("_")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.