What is the best way to send multiple input box values to a function set in the controller?

I am attempting to send the values of two input boxes to a single controller function.

   <div class="container" ng-controller="MainCtrl">
        <div class="row">
            <div class="col-lg-6">
                <input type="text" name="regex" class="form-control" placeholder="Regex"
                       ng-model="do_parsing.data"
                       ng-model-options="{ getterSetter: true }">

            </div>
            <div class="col-lg-2">
                <input type="text" name="modifier" class="form-control" placeholder="modifier">
            </div>

        </div>
        <hr>
        <div class="form-group">
            <textarea class="form-control" id="input_data" rows="10" style="width: 555px; height: 214px"
                      placeholder="Insert your test string here"
                    ng-model="do_parsing.data"
                       ng-model-options="{ getterSetter: true }"></textarea>
        </div>
        <div class="form-group">

            <pre> <span ng-bind="do_parsing.data()"></span></pre>
        </div>
    </div>

This is my controller.js file,

app.controller('MainCtrl', function ($scope) {
    var _data = '';
    $scope.do_parsing = {

        data: function(newData) {
            if (newData != undefined) {
                alert(newData);
            }
            return arguments.length ? (_data = newData) : _data;
        }
    };
});

I aim to pass the values of the first input box and the text area to the do_parsing function. I want the function to then return the parsed data to the pre tag. Essentially, this do_parsing function should be triggered whenever the value of the first input box and the text-area changes. I have attempted the above approach, but it displays the same value in all three places.

Jsfiddle

Answer №1

To connect the input field and textarea to specific fields on the $scope object, you can follow these steps:

<input type="text" 
       name="regex" 
       class="form-control" placeholder="Regular Expression"
       ng-model="inputValue"
       ng-model-options="{ getterSetter: true }" />

Next, for the textarea:

<textarea class="form-control" 
          id="input_data" 
          rows="10" 
          style="width: 555px; height: 214px"
          placeholder="Enter your test string here"
          ng-model="textareaValue"
          ng-model-options="{ getterSetter: true }">
</textarea>

Then, assign a function to display the result in a span:

<pre>
    <span ng-bind="do_parsing.data()"></span>
</pre>

In your controller, define the following function:

app.controller('MainCtrl', function ($scope) {
    $scope.inputValue = '';
    $scope.textareaValue = '';

    $scope.do_parsing = {
        data: function() {
            // Perform an operation with the two values
            // Display the result in the span

            return $scope.inputValue + ' | ' + $scope.textareaValue; 
        }
    };
});

Check out this live demo.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dynamic route fails to return value for ID search

Currently, I am testing by creating an array of users containing their respective IDs and names. There is also a button that triggers an onclick function to add the element's ID to the page's URL in order to establish a dynamic route. However, wh ...

What is the reason behind the widespread adoption of Node.js and NPM for the compilation of JavaScript libraries by

The widespread adoption of Node.js and NPM in the JavaScript community has left me perplexed. Why must we rely on such drastic measures? What issues are these tools aiming to resolve for us? [Update] I feel like my original question missed the mark. Fra ...

How to effectively test a form submission with an external API using React, Jest, and Enzyme?

I have integrated a geocoding API to verify address submissions on a form. Upon submitting the form, the geocoding API is called first, followed by validation of the remaining form fields. class Form extends React.Component { geocode = (streetAddress, cit ...

Include a personalized header in $http

In my factory, I have multiple functions that follow this structure: doFunction: function () { return $http({ method: 'POST', url: 'https://localhost:8000/test', data: {}, headers: { "My_Header" ...

The isOpen State in AngularJS Accordion Component

Is it possible to determine if an accordion-group is open or closed without using the isOpen directive? I am curious if there is a way to access this information solely through HTML. I have experimented with two-way binding to store the state, but this app ...

Storing a Promise in a Variable in React

As a newcomer to JavaScript/React, I am finding it challenging to grasp the concept of using Promise and async. To illustrate, consider the API call getSimById in a JS file that returns a Promise: export function getSimById(simId) { return fetch(simsUrl ...

"Duplicate content issue with ng-transclude causing element to render twice

Why is the transcluded directive displaying Name inside directive = Frank twice? I believed I had a good grasp on transclusion, but this specific scenario has left me puzzled. Check out this fiddle for more details <div ng-app="myApp" ng-controller=" ...

Having trouble passing a JavaScript variable through AJAX requests

In this particular script, I am encountering an issue where the variable "encrypted" is expected to be sent via ajax to upload.php, however I am unable to achieve this. Oddly enough, if I substitute the "encrypted" variable with another variable in the a ...

Changing the content of a form with a personalized message

I am currently working on a Feedback modal that includes a simple form with fields for Name, rating, and comment. After the user submits the form, I intend to display a custom message such as "Your feedback has been submitted." However, I am facing an issu ...

Tips for managing content and tables that exceed their container's boundaries

On my webpage, I have a slide-out sidebar that shifts the other contents and causes overflow with a scrollbar. I want the content to remain inside the window and adjust according to the available space. Image of Slide-out Sidebar As seen in the image, t ...

Creating a dynamic start page for Progressive Web Apps (PWA) involves determining the

Recently, I developed a project called "Progressive Web App" using JavaScript and Visual Studio 2017. One key element of the project is the file "package.appxmanifest", which defines the StartPage. I am curious to know if there is a way to dynamically se ...

Removing one element from an array with mongoose

As a newcomer to web development, I am currently working on creating a todo app. Below is the schema and model that I have: const tdSchema = new mongoose.Schema({ category: { type: String, required: true, unique: true }, tds: { type: ...

What is the best way to determine the property type dynamically in TypeScript based on the value of another property?

I have been working with Polymorphic relationships and currently have the following TypeScript interface defined: interface SubjectA {} interface SubjectB {} interface SubjectC {} enum SubjectType { SubjectA = 'Subject A', SubjectB = 'S ...

The Jqueryui image link is not displaying the image despite no error being reported

Can anyone help me figure out what I'm missing? I'm currently working with ASP.NET MVC 5 and have downloaded the JqueryUI combined via Nuget package. Despite no error references for css/js files, the close button is still not showing in the ima ...

Enhance your JavaScript skills by deserializing objects and seamlessly integrating new methods

Currently in my Javascript code, I am utilizing localStorage. Since objects cannot be directly stored in it, I am using JSON.stringify to serialize them before saving. Within localStorage, I am storing the entire game state, where some of the sub-objects ...

Dynamically inserting a new row into a table with the power of jQuery

Currently working on dynamically adding rows to a table through jQuery in my project that follows MVC design pattern. I have set up a loop for the added rows, but need help with the script. Here is my code snippet for the loop : <?php $viewTableR ...

Is it possible to determine if an AJAX request is still ongoing when a user returns to a webpage using jQuery or JavaScript?

Users on a specific page must click a 'generate' button to create details for a record. When the button is clicked, an ajax request is triggered to generate the record, which may take some time. $("#generate_record_button").on("cl ...

Locating a user by their ID within a collection in Meteor can lead to some unexpected behavior

I have a requirement where I only want to allow users to insert a document if their email is verified. In an attempt to achieve this, I wrote the following code snippet. Events.allow({ insert: function (userId, doc) { var user = Meteor.users.f ...

Managing the scrolling direction horizontally with waypoints.js

As I work on creating a custom wizard form with waypoints, I've encountered an interesting issue that has left me puzzled. In my sample CODEPEN, you can see two pages of the wizard process to better understand the problem. Upon clicking the forward ...

Locate specific phrases within the text and conceal the corresponding lines

I have a JavaScript function that loops through each line. I want it to search for specific text on each line and hide the entire line if it contains that text. For example: <input id="search" type="button" value="Run" /> <textarea id ...