使用自定义json创建角形式并获取数据

时间:2014-04-03 18:22:05

标签: json angularjs angularjs-directive angularjs-scope

您好我正在使用角度js来构建我的应用程序。生成动态表单时遇到一个问题。 我的json是:

{
   lines :[ {
          fields:[{
               fieldType : "input",
               labelName : "Test Label",
               modelName : "testInput"
            },{
               fieldType : "select",
               labelName : "Test Label",
               options : "['opt1','opt2','opt3']",
               modelName : "testSelect"
             },{

               fieldType : "checkbox",
               labelName : "Test Label",
               modelName : "testCheckbox",
               options : '[{'key':'key1','value':'value1'}]"
     }]
}

这只是示例json。实时,它是更复杂的类型。我为所有的Html元素创建了单独的组件,如“input”,“select”,“checkbox”,“radio”...... 我正在使用所有输入元素的指令。我从父表格范围到指令的细节很少。 例如:

主要指令

<ui-form form-option='uiFormOption'></ui-form>
Above is the main directive. i am passing above json in this Main Directive.

在指令中,我正在迭代所有行,并调用相应的指令来根据字段类型填充组件。

在这里,在Html组件指令中创建隔离范围我需要太精细。

当我保存时,我需要在ParentScope中获取单个对象中的所有数据 就像是           $ scope.myFormData = {}; //父范围控制器。 当我点击保存时,必须收集所有数据。我的问题是child指令中的$ scope。所以模型地图不了解父母。如何实现这一目标......

2 个答案:

答案 0 :(得分:0)

我不确定我是否正确地理解了你的问题,但是我脑海中浮现出两件事,我相信你已经尝试过了,但以防万一:

1-因为您能够将数据获取到html组件,将数据直接绑定到您从中收集数据的对象,并且因为它已经可以在任何地方访问,所以您可以使用它来格式化任何响应你需要。

2-如果没有其他选项可用,你可以使用$ scope。$ root,我知道这不是最好的事情,但是为你的所有元素获得共享范围总是很糟糕。

我希望这就是您所寻找的,如果不是,请发表评论。

答案 1 :(得分:0)

如果你需要显示动态表单,那么通过API获取这种json对象:

def get_distinctive_fields():
    """
    By these fields a people can be distinguished
    from one another.
    """

    field_list = ['last_name_ru',
                  'premarital_surname_ru',
                  'first_name_ru',
                  'patronymic_name_ru',
                  'nickname_ru',
                  'year_of_birth', ]

    return field_list

在给定的响应中,采用JSON格式。 所以你可以轻松地将这些数据与我们的html

绑定