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

In Javascript, ensuring a stopping condition for a recursive function that is looping through JSON data

I am having trouble figuring out how to set the break condition for this recursive function. Currently, the function is causing the browser to freeze (seems to be stuck in an endless loop). My goal is to create a series of nested unordered lists based on ...

Is it possible to create a Facebook reveal tab using either Javascript or .NET?

As a developer who jumped into Facebook development just before the recent changes, I am feeling lost when it comes to building apps now. I see many questions similar to mine about creating fan-gating features using Javascript only. Is there an up-to-date ...

inside the connect module, the res._renderHeaders function is located

Currently, I am delving into the patch.js file within the connect module. In it, I found some intriguing code snippets: var http = require('http') , res = http.ServerResponse.prototype , setHeader = res.setHeader , _renderHeaders = res._re ...

Guide on integrating Select2 with webpack

I recently acquired the select2 node module with this command: npm install select2 After adding it to my app.js: require('select2')($); Although no errors appear when I use webpack, upon opening the application, I encounter: Uncaught TypeEr ...

Changing the i18n locale in the URL and navigating through nested routes

Seeking assistance to navigate through the complexities of Vue Router configurations! I've dedicated several days to integrating various resources, but have yet to achieve successful internalization implementation with URL routes in my unique setup. ...

Tips for managing open and closed components within a React accordion and ensuring only the clicked component is opened

Unique Accordion component: const CustomAccordion = (props: AccordionProps) => { const { label, levels, activeId, id } = props const [isExpand, setIsExpand] = useState(false) const onPress = useEvent(() => { setIsExpand( ...

Guide on sending two values from a form using Ajax and the POST method

How can I send two values, A and B, from a form to check.php using Ajax with the POST method? In the code snippet below, I managed to send two input values to check.php and assign them to variables $A and $B. However, I want to accomplish this without ref ...

Utilizing child component HTTP responses within a parent component in Angular: a comprehensive guide

As a newcomer to Angular, I find myself struggling with http requests in my application. The issue arises when I have component A responsible for retrieving a list of IDs that need to be accessed by multiple other components. In component B, I attempted t ...

Is pl/pgsql block code supported by postgres-nodejs?

I am attempting to define a custom UUID variable that can be utilized across multiple queries within a transaction. Initially, I attempted using a JavaScript variable which ultimately defeated the purpose of declaring the variable on the server side. The ...

Trouble with AngularJS Smart Table when dealing with live data streams

Currently, I am facing a scenario where I am utilizing angularJs smart table for filtering. Here is the HTML code: <section class="main" ng-init="listAllWorkOrderData()"> <table st-table="listWorkOrderResponse"> <thead> ...

Encountered an issue while trying to install using the command npm install react router dom

For a project I'm working on, every time I attempt to use this command, an error message appears and the installation fails. I've tried multiple commands with no success. Here is the specific error message: npm ERR! code 1 npm ERR! path C:\ ...

Overflow of text arranged horizontally within a span element situated inside a div container

I am currently working on developing a ticketing system that involves using nested div elements to organize content. Each ticket is represented by a main div containing various other nested divs and media such as images. While the functionality of the sys ...

Issue with Bcrypt comparison resulting in constant false output

Attempting to implement password verification using bcryptjs, const bcrypt = require('bcryptjs'); login(post){ this.uid = post.uid; this.pin = post.pin; this.getUser(this.uid); if(this.checkUser != undefined ...

Assign the Firebase token to the JavaScript cookie value

Can a cookie store a token value? In my setup with js-cookie, Firebase auth/firestore, and Next.js, I am setting my cookie within the handleUser function like this: const handleUser = async (rawUser) => { if (rawUser) { const user = await fo ...

Button click causing TextField to print incorrectly

I am working on implementing a feature in my react application where users can input a search term, and upon pressing the button, it will be used to perform a search. The text input field and button are utilizing material-ui components. At this stage, I si ...

Show a table with rows that display an array from a JSON object using JavaScript

My current code includes JSON data that I need to display in table rows, but I'm struggling to understand how to do so effectively. The output I am currently seeing is all the rows from the array stacked in one row instead of five separate rows as in ...

Error: Unable to locate bundle.js when attempting to refresh the page with a specific ID in the URL

I encountered an issue where I tried redirecting a user to their profile page to display the profile information corresponding to it. Let's say we have http://localhost:8080/user/1 as an example. Upon redirecting the user using the navbar link, the pa ...

Traversing through pair of arrays simultaneously using forEach loop in JavaScript

I am trying to create a for loop that simultaneously iterates through two variables. One is an array named n, and the other, j, ranges from 0 to 16. var n = [1,2,3,5,7,8,9,11,12,13,14,16,17,18,20,21,22]; var m = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; ...

Navigate through a list of data in JSON format

After successfully implementing a jQuery AJAX call, I encountered difficulty in parsing the returned value. Working with a MySQL database, I am returning a PHP array() to my jQuery AJAX function using echo json_encode($reservationArray); Upon appending th ...

Tips for generating a node for the activator attribute within Vuetify?

Vuetify offers the 'activator' prop in multiple components like 'v-menu' and 'v-dialog', but there is limited information on how to create a node for it to function correctly. The documentation states: Designate a custom act ...