AngularJS. Converting form fields to json

后端 未结 1 1751
不知归路
不知归路 2021-01-31 06:25

Trying to understand how could be possible to write a function, (directive / controller) that would transform all my form inputs to json with current value that the

1条回答
  •  天涯浪人
    2021-01-31 06:37

    ng-model does it for you. A scope variable will be created if you haven't already created it yourself

    function myController($scope){
        $scope.submitMyForm=function(){
            /* while compiling form , angular created this object*/
            var data=$scope.fields;  
            /* post to server*/
            $http.post(url, data);        
        }
    
    }
    

    If you have the object to start with in your scope angular will 2 way bind to the input, so any values that are initially set in the scope object will show up in the input

    0 讨论(0)
提交回复
热议问题