Amzad Khan Singh Amzad Khan Singh - 5 months ago 10
AngularJS Question

How to submit angular js form using ng-click

I am trying to create a from using json file. So I have a json file I am calling it into my html using angular js I am getting problem when I click on save button which is using ng-click , Here I am trying to use the array of user like this:

<div ng-repeat="x in myWelcome.definition.attributes">
<div ng-repeat= "y in x.children ">
<div ng-if ="y.rm_type_name == 'DV_TEXT'">
<input type="{{ y.node_id }}" name="{{ y.node_id }}" ng-model="user[y.node_id]">
</div>
<div ng-if ="y.rm_type_name == 'DV_CODED_TEXT'">
<input type="{{ y.node_id }}" name="{{ y.node_id }}" ng-model="user[y.node_id]">
</div>

</div>
<h1>You entered: {{user}}</h1>

</div>
<input type="submit" ng-click="update(user)" value="Save" />


And my controller is looking like this :

$scope.update = function(user) {
console.log(user);
};


I am getting the undefined in my console here is the plunker :
https://plnkr.co/edit/62g4YhkowQtwkyBOPKi5?p=preview

Answer

You need to add only one line and I think that will work use this inside your controller and no more changes :

 var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $http) {
      $scope.master = {};
      $scope.user = {};
      $scope.update = function(user) {
        $scope.master = angular.copy(user);
        $scope.user = {};
        console.log(user);
      };
      $http.get("data.json")
      .then(function(response) {
          $scope.myWelcome = response.data;
      });
    });

You only need to add this in your controller $scope.user = {}; and it will work