AngularJS。转换表单字段JSON [英] AngularJS. Converting form fields to json

查看:176
本文介绍了AngularJS。转换表单字段JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图了解怎么可能写一个函数(指令/控制器)这将改变我所有的表单输入与当前值的JSON他们必须 JSON

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 they have to json.

的JSON将具有类似于一个格式:

The json would have a format similar to that:

{    
    fields: [
          {field1: value1},
          {field2: value2},
          {field3, value3}
        ]    
}

凡从开始至少..没有的jQuery 申请?

推荐答案

NG-模型会为你。如果您尚未创建自己成为一个变量的范围将创建

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

<form name="myForm" ng-submit="submitMyForm()">
    <input ng-model="fields.name"  />

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

}

如果你要开始在你的范围的角度将在2路绑定到输入端的对象,这样在范围对象最初设置的任何值将在输入显示

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

这篇关于AngularJS。转换表单字段JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆