How to pass an Array to AngularJS?

How to pass angularjs array data from asp.net webform to an angularjs function

  • I'm a beginner with AngularJS and I'm trying to send data to an AngularJS controller from an asp.net webform. More specifically, I have one or more rows of data in an Asp.net Web Form. In each row I have 4 properties such as first name, last name, height, and weight. What I'm trying to do is create an array in an AngularJS Controller and pass each row of data into the Controller and push it onto an array. So I would have something like this: Item(0).firstname Item(0).lastname Item(0).height Item(0).weight Item(1).firstname Item(1).lastname Item(1).height Item(1).weight etc. So far I have this in my AngularJS controller: // Item List Arrays $scope.beneficiaries = []; $scope.AddBeneficiary = function (b) { // Add a Item to the list $scope.beneficiaries.push({ firstName: b.firstName, lastName: b.lastName, relationship: b.relationship, percentage: b.percentage }); } And I'm trying to pass the data from my web form rows like this: ng-model="benficiaries[0].firstName"> and then in the button I have ng-click="AddBeneficiary(beneficiary[0])" Obviously, this is not working. Any help is greatly appreciated. Thanks, Pete The markup looks like this. For example, for first name: First Name: <br /> <asp:TextBox ID="uiBeneFirstName1" runat="server" MaxLength="20" CssClass="form-control" ng-disabled="model.uiBeneRelationship1 == '2'" TabIndex="35" ng-model="benficiaries[0].firstName"></asp:TextBox> </div> and the button click where I'm trying to pass the first firstname to the controller currently looks like this: <button runat="server" ng-click="AddBeneficiary(beneficiary[0])">Add Beneficiary</button>

  • Answer:

    Your $scope.beneficiaries is an empty array at the start so you cannot bind it to an input like <input type="text" name="firstName" ng-model="beneficiaries[0].firstName"/> because there is no first element $scope.beneficiaries[0]. You need a different model in your form, example: <input type="text" name="firstName" ng-model="formData.firstName"/> and your add button would have ng-click="AddBeneficiary(formData)"

Pete at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.