1

I have a a string 'I like orange, blue, black, pink, rose, yellow, white, black'. Is it possible to replace words yellow and black with input fields, so I could type in my own colours?

const a: string = 'I like orange, blue, black, pink, rose, yellow, white, black';
const b: string =['black', 'yellow'];

So I would like to have something like this link

Is it possible to do this with javascript or angular2?..

Please, help me...

Here is full code

part2.component.ts

import { Component, OnInit } from '@angular/core';

import { DataService } from '../sample02-simpleService/data.service';

@Component({
  moduleId: module.id,
  selector: 'part2',
  template: `
    <p>{{text}}</p>
    <p>{{itemsSource}}</p>
  `,
  providers: [DataService]

})
export class Part2Component implements OnInit {
  text = 'Hallo';
  public itemsSource: string;
  public itemsSource2: string[];
  public abc: string[];
  constructor(public dataService: DataService) {
  }

  ngOnInit() {
    this.abc = this.dataService.getData3();
    this.itemsSource = this.dataService.getData2();
    this.itemsSource = this.itemsSource.replace(new RegExp(this.abc.join('|'), 'g'),  function myFunction(){return document.write('<input>'); });

  }
}

And I get data from DataService, so from here

import { Injectable } from '@angular/core';

          @Injectable()
export class DataService {
              getData2() {
              let items3: string = 'I like orange, blue, black, pink, rose, yellow, white, black';
              return items3;
            }
            getData3(){
              let items4: string[] = ['black', 'yellow'];
              return items4;
            }
          }
5
  • I guess it must be something like a = a.replace(new RegExp(b.join('|'), 'g'), function myFunction(){return document.write('<input>'); }); Commented Jun 13, 2017 at 17:59
  • What is wrong with the answer you just posted in the comments? Commented Jun 13, 2017 at 18:06
  • @Arrow, it doesnt work, it says "type void is not assignable to type string" Commented Jun 13, 2017 at 18:19
  • can you post how you are implementing the function in your code? It will provide more context and help solve your problem. It will also prevent this question from being marked as a duplicate. Commented Jun 13, 2017 at 18:21
  • 1
    @Arrow Sure, I edited my question, so the full code is now there Commented Jun 13, 2017 at 18:46

2 Answers 2

0

Here is the code from component part:

a: String = 'I like orange, blue, black, pink, rose, yellow, white, black';
b: String[] = ['black', 'yellow'];

constructor() { }

ngOnInit() {
    this.b.forEach(str => {
        let regExp = new RegExp(str.toString(),'g');
        this.a = this.a.replace(regExp, '<input/>');
    });
 }

The above function will set exact html string that you wants to implement

Next step is here :

Inject <input> in innerHTML angular 2

Angular 2 doesn't support injection of input field directly , so you have to follow this link.

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

2 Comments

I have read carefully read the solution on the link ( I mean how to inject input in HMTL), but can't apply to my case - it doesn't work, and I really don't know how to make it work...could you maybe give me a hint, what should I change in the example, that was provided by link, to make it work?..
Will you please set plunker ?
0

Updated:

After understanding your requirement, I have changed implementation technique and updated answer accordingly. Instead of using angular-pipe(recommended to use for display purpose) I have created simple component as follows

import { Component, EventEmitter, OnInit, Input, Output } from '@angular/core';

@Component({
  selector: 'fill-in-blank',
  template: `
  <ng-container *ngFor="let text of texts; let i = index">
    {{text}}<input *ngIf="i!==texts.length-1" type="text" [(ngModel)]="value[i]" (ngModelChange)="onValueChange()">
  </ng-container>
  `
})
export class FillInBlankComponent implements OnInit {

  @Input() textline: string;

  @Input() fields: string[];

  @Output() valueChanged = new EventEmitter();

  texts: string[];

  value: string[] = [];

  constructor() {}

  ngOnInit() {

    let regex = null;
    if (this.fields && this.fields.length > 0) {
      regex = new RegExp(this.fields.join("|"), 'gi');
    }
    this.texts = this.textline.split(regex);
  }

  onValueChange(index) { // Updated According to requirement
    this.valueChanged.emit({ index: index, value: this.value[index] });
  }
}

Use above component where you want to render it like this

<fill-in-blank [textline]="a" [fields]="b" (valueChanged)="printValue($event)"></fill-in-blank>

To build your array of answer you need to call following method in ngInit. This is how your parent component should look like

export class ParentComponent implements OnInit {

  a: string = 'I like orange, blue, black, pink, rose, yellow, white, black';
  b: string[] = ['blue', 'black'];
  ans: string[] = [];

  constructor() { }

  ngOnInit() {
    this.createAnswers();
  }

  printValue($event) {
    if (this.ans[$event.index] === $event.value) {
      console.log('correct input', $event);
    }
  }

  createAnswers() {

    let ansWithIndex = [];
    this.b.forEach(value => {
      let index = 0;
      let startIndex = 0;
      while ((index = this.a.indexOf(value, startIndex)) > -1) {
        ansWithIndex.push({ index: index, value: value });
        startIndex = index + 1;
      }
    });

    this.ans = ansWithIndex.sort((v1, v2) => v1.index > v2.index ? 1 : -1).map(v1 => v1.value);
  }
}

Make sure you have added FillInBlankComponent in Module Declaration section.

7 Comments

This works perfect, you answer is brilliant, thanks you!;)
I have one little question, it seems I can't read data and store it to array from this inputs, I tried several methods to achieve this, but they do not work and I don't know why...Do you know, how to solve it?...
@NataliaLomova Based on your input I have updated my answer. If it works for you, up vote and mark it as accepted :)
Thank you! I have just a little question, about printValue...what should I write in this method - It doesn't print anything yet...
is it possible to slightly change FillInBlankComponent in such way, that if I type in input fields words, that were there in the beginning, black and yellow, it alerts "OK", otherwise "No". Do I need new function to check the values or is it possible to make everything in FillInBlankComponent ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.