What is the syntax for defining "skills[]" in an AngularJS input field of type text?

When working with PHP, we often use name="skills[]" to retrieve data from a form as an array. How can this be achieved in AngularJS? For example, in PHP:

<input type="text" name="skills[]" />

I am attempting to achieve the same functionality in AngularJS but encountering syntax errors. My attempt looks like this:

Here is my AngularJS code:

<input type="text" name="skillname" required data-ng-model="c.skills[].skillname" class="small">
<input type="text" name="skillname" required data-ng-model="c.skills[].skiilname" class="small">

Any assistance would be greatly appreciated. Thank you.

Answer №1

Within Angular, the hierarchy ensures that your UI conforms to the data structure. It is important to maintain a skills array in your controller.

app.controller('MainCtrl', function($scope) {
  $scope.skills = [
    {},
    {},
    {}
  ];
});

The ng-repeat directive can be used to display each skill from the array on the page.

<div ng-repeat='skill in skills'>
  <input ng-model='skill.name' />
</div>

This method allows for easy addition of new textfields by simply adding a new element to the array using $scope.skills.push({}). There is no need to manipulate DOM elements directly. This approach aligns with the principles of Angular development.

Demonstration: http://plnkr.co/edit/Uqldnst3gnF07SJGV6Bn?p=preview

Answer №2

Check this out:

Here is your controller setup:

app.controller('MainCtrl', function($scope) {
   $scope.skills = [];
  $scope.formData={
   skillname:[]
      };
  });

Now, take a look at your HTML code:

<div ng-repeat="skill in skills">
<input type="text"  required ng-model="formData.skillname[$index]" ng-init="formData.skillname[$index]=skill.skillname" class="small">
  </div>

If you prefer not to use ng-repeat, you can try this alternative code:

<input type="text"  required ng-model="skillname[0]" ng-init="skillname[0]=skills[0].skillname" class="small">
<input type="text" required ng-model="skillname[1]" ng-init="skillname[1]=skills[1].skillname" class="small">

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

Using HTTP POST to subscribe and implementing a try-catch block

When using Angular2's http.post, I encountered an issue where the headers were not sent to the CORS server. To address this, I attempted to create a loop that would retry the request until it succeeds. However, this resulted in an endless loop. How ca ...

Unexpected behavior in Angular service's value exposure

I'm encountering an issue accessing values in my service from the controller. The structure of my service is as follows: angular.module('someApp').factory('someSvc', SomeSvc); function SomeSvc($http) { var colors = []; fu ...

Creating a Single Button Photo Upload Functionality in ASP.net Using C# From a GridView Row

I am attempting to enable photo uploading from a row within a GridView, where the file name is generated based on the details of that row. While I have a functioning version of this feature, it does not seem to work consistently across different browsers. ...

Is there a standardized method for obtaining a date in the format of six digits as YYMMDD?

In my current project, I'm developing a function that generates a list of dates represented in a 6-digit format beginning from the present day up until August of 2018. The desired output should resemble the following: [190322, 190321, 190320, ...] I ...

Revamping array elements in React

Once I added an element to the array, I needed to update this array by doubling all elements except for the one that was just added. Despite trying setArray([]) before retrieving the array from the database, it didn't seem to work... const [array, se ...

Using Titanium for Android to Display High-Quality Images

My goal is to showcase a high-resolution image (minimum size of 2000x2000) on an Android device using Titanium, allowing users to scroll around it similar to a scroll view on iOS. However, I am aware that Android does not offer the same type of scroll view ...

Can you guide me on how to programmatically set an option in an Angular 5 Material Select dropdown (mat-select) using typescript code?

I am currently working on implementing an Angular 5 Material Data Table with two filter options. One filter is a text input, while the other is a dropdown selection to filter based on a specific field value. The dropdown is implemented using a "mat-select" ...

Click on the <a> tag to submit the form

I currently have: <ul> <form name="myform" action="Users" id="myform" method="post"> <li><a id="target" name="target" value='A' href="#">A</a></li> <li><a id="target" name="target" value=&ap ...

Is it possible to use a full-width material-ui Button inside a Badge component?

Within a grid, I had initially used fullWidth on a Button to make it expand and fill the container. Everything was functioning correctly until I enclosed the Button in a Badge element. Now, the fullWidth property is not being applied, and the button rever ...

Guide to Repairing Uncaught TypeError: players.setAttribute is not a recognized method?

I'm a beginner and encountered an error saying Uncaught TypeError: players.setAttribute is not a function when submitting an action. How can I solve this issue? Please share your insights. ''' //selecting all necessary elements const s ...

What is the correct way to utilize browser actions for sending keys with the "?" symbol in Protractor?

I'm facing an issue in my tests with a particular line of code browser.actions().sendKeys(Key.chord(Key.CONTROL, '?')).perform(); Interestingly, it works fine with another symbol. For example: browser.actions().sendKeys(Key.chord(Key.CONT ...

Modifying table background color using AJAX and jQuery?

Scenario: My web page is designed to automatically search for a specific cell input by the user. If the cell has been input with a value, the table's background color will turn red; otherwise, it will remain green. Currently, the table has not been p ...

Instructions for resetting a DataTable in JQuery that was originally built using an append operation

Encountered a problem with restarting my datatable after populating it with data from a web service. Everything fills up correctly, but upon clicking the Search Button again, the old table header remains and the function fails to include the new results in ...

The input field will be in a read-only state if it contains a value from the database. However, it will be editable if the value

Hello everyone, I am a newcomer to this community and would greatly appreciate your help. I am encountering an issue with the following lines of code: <input type="text" class="form-control" id="fines" name="fines&quo ...

The iPad Air 2 experiencing issues with rendering on three.js

Recently, I've transitioned to using an iPad Air 2 for work and decided to explore equirectangular panoramas. While these panoramas work perfectly on my desktop, I was excited about utilizing the device orientation features on my iPad/iPhone. However ...

How can one access a parent component's property within a child component in AngularJS 1.5?

My current issue involves a parent and child component, where I need to toggle the display of certain elements using the <ng-if> directive based on the value of a property called isTrue. However, I am facing difficulties in achieving the desired func ...

Using Swagger with Next.js

Is it possible to generate Swagger documentation for API routes in NEXT.js? I am working with Next.js for both front-end and back-end tasks, and I am interested in creating a Swagger documentation for the APIs implemented in Next.js. ...

Manipulating input dates in AngularJSIn AngularJS, we can easily set the value

I'm having trouble setting a date field in my code. Despite trying to follow the example provided in the AngularJS documentation, nothing is showing up in the "departure" field. Here is what I have: HTML: <input type="date" name="departure" ng-mo ...

How can I restrict the selection of only one checkbox within an iframe window?

Here is my JavaScript snippet: var iframe = document.getElementById('pltc'); iframe.contentWindow.document.open('text/htmlreplace'); iframe.contentWindow.document.write('<input type="checkbox" name="tc0">Yes<input type="c ...

Exploring ways to incorporate various classes into my validate() elements using jQuery

I'm currently using the jQuery method validate to verify this particular form: <form id="emailRecover"> <div class="row light-field-container error-container"> <input type="text" id="dniPassword" name="dniPassword" requ ...