Post a list of selections in an array to server using ng-model

。_饼干妹妹 提交于 2019-12-06 14:38:40

I think you want to post a data structure like this to the server?

{
    employee: {
        name: "Tushar",
        projects[{
           name: "Angular App"
        }]
    }
}

You could use a submit handler in the controller:

function EmployeeCtrl($scope) {
    $scope.employee = {projects:[]};  // Initialise data structure
    $scope.submit = function() {
      console.info($scope.employee);
      // Convert employee data structure to string an POST to server
      postToServer(JSON.stringify(angular.copy($scope.employee)));
    }
}

angular.copy removes the $$hashKey property on the project items.

https://github.com/angular/angular.js/issues/1875

Where the form would look like this:

<form ng-submit="submit()">
    <input ng-model="employee.name">
    <div ng-repeat="project in employee.projects">
        <input ng-model="project.name">
    </div>
    <button type="submit">Save</button>
</form>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!