1

I seem to have a problem passing input data from HTML's <select> element to Angular Forms itself.

Here's my code first.

Filename: home-page.component.html

<form [formGroup]="rForm" (ngSubmit)="addPaste(rForm.value)">

    ...

    <div class="input-field col s12">
        <select formControlName="pasteSyntax">
            <option *ngFor="let choices of pasteSyntaxChoices" [value]="choices.value">{{ choices.text }}</option>
        </select>
        <label>Syntax Highlighting</label>
    </div>

    ...

</form>

Filename: home-page.component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
    selector: 'app-home-page',
    templateUrl: './home-page.component.html',
    styleUrls: ['./home-page.component.css']
})

export class HomePageComponent implements OnInit {

    rForm: FormGroup;
    paste: any;

    ...

    pasteSyntax: string = '';

    ...

    pasteSyntaxChoices = [
        { value: "plain", text: "Plain Text" },
        { value: "html", text: "HTML" },
        { value: "css", text: "CSS" },
        { value: "js", text: "JavaScript" },
        { value: "php", text: "PHP" },
        { value: "perl", text: "Perl" },
        { value: "go", text: "Go (Golang)" }
    ];

    constructor(private fb: FormBuilder) {
        this.rForm = fb.group({

            ...

            'pasteSyntax': [null, Validators.required]

            ...

        });
    }

    addPaste(paste) {

        ...

        this.pasteSyntax = paste.syntax;

        ...

        console.log(paste);
    }

    ngOnInit() {}

}

Additional Info:

@angular/animations: 4.2.4
@angular/common: 4.2.4
@angular/compiler: 4.2.4
@angular/core: 4.2.4
@angular/forms: 4.2.4
@angular/http: 4.2.4
@angular/platform-browser: 4.2.4
@angular/platform-browser-dynamic: 4.2.4
@angular/router: 4.2.4
core-js: 2.4.1
rxjs: 5.4.2
zone.js: 0.8.14

Expected Output (From console.log):

Object
    pasteSyntax: "plain"

Current Output (From console.log):

Object
    pasteSyntax: null

Can anyone tell me where did I go wrong or did I use the wrong syntax?

7
  • What is paste.syntax? Is this really supposed to be paste.pasteSyntax? From what I can see, you do not have a field named syntax in the form group. Commented Nov 1, 2017 at 11:36
  • @R.Richards, nope. Everything in my actual code works fine so that isn't the problem. Commented Nov 1, 2017 at 11:59
  • Try adding #rForm inside the form tag. I think that what you are passing to addPaste isn't quite right (obviously, it is null). Commented Nov 1, 2017 at 12:02
  • @R.Richards, that actually crashed my whole form lol. See: Console Log Commented Nov 1, 2017 at 13:36
  • That was not my intention! :) I was hoping that would get you the reference you needed. Commented Nov 1, 2017 at 13:40

1 Answer 1

1

You problem is this line in your addPaste:

this.pasteSyntax = paste.syntax;

it should be:

this.pasteSyntax = paste.pasteSyntax;

But as mentioned by Nilandri, you don't need to variable, you can access the value from the form in your template with, or then print the pasteSyntax property when you submit your form with rForm.value as parameter:

addPaste(value) {
  console.log(value.pasteSyntax);
  console.log(this.rForm.get('pasteSyntax').value);
  // or
  console.log(this.rForm.controls.pasteSyntax.value)
}

DEMO

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

9 Comments

I do believe that it is the same thing as console.log(paste) though more specific. Tested your answer, still returned null from the console. Any other ideas?
Well I just like to use the variables available, instead of having 'unnecessary' variables, but of course you can use it :) The answer I provided should work just fine: stackblitz.com/edit/angular-vcavs7?file=app/app.component.ts
@EtosticityRammington updated answer, noticed the error when looking closer, but as said, the answer I provided before editing should work tho :)
Actually, your demo works! Code compared with my side and yours, are totally the same. But I just can't work out why isn't it working. Is it possible to communicate somewhere else for this? Thanks! :D
Ah... Hmm alright, thanks for the help anyway, appreciate it! :D
|

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.