2014-04-03 27 views
0

嗨我正在使用角js構建我的應用程序。生成動態表單時遇到一個問題。 我的json是:使用自定義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元素創建單獨的組件,如「輸入」,「選擇」,「複選框」,「收音機」...我使用指令爲所有輸入元素。我從表格的父母範圍中抽取了一些細節到指令。 爲如:

主要指令

<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 = {}; //父級作用域控制器。 當我點擊保存,所有的數據必須收集。我的問題是兒童指令中的$範圍。所以模型地圖不知道給父母。如何實現這一目標?

回答

0

我不知道如果我理解正確你的問題,但在我的腦海裏突然出現兩件事情,我相信你嘗試過,但以防萬一:

1,因爲你是能夠將數據獲取到html組件,將數據直接綁定到您要從中收集數據的對象,並且由於它已在任何地方都可訪問,因此您可以使用它來格式化所需的任何響應。

2-如果沒有其他選項可用,您可以使用$ scope。$ root,我知道這不是最好的做法,但它總是爲所有元素獲取共享作用域。

我希望這是你正在尋找的,如果不是,請評論。

0

如果需要顯示動態形式通過API,以便獲取這種JSON對象的:

{ 
"status": "true", 
"statusCode": 200, 
"response": [ 
    { 
     "sort_order": 0, 
     "control_name": "ion-select", 
     "label": "Select master OLT", 
     "type": "select", 
     "placeholder": "Select master OLT", 
     "name": "E0selectoption_master_olt", 
     "readonly": "True", 
     "required": "required", 
     "maxlength": "", 
     "minlength": ""   
    }, 
    { 
     "sort_order": 1, 
     "control_name": "ion-select", 
     "label": "Select OLT port", 
     "type": "select", 
     "placeholder": "Select OLT port", 
     "name": "E1selectoption_olt_port", 
     "readonly": "True", 
     "required": "required", 
     "maxlength": "", 
     "minlength": ""   
    }, 
    { 
     "sort_order": 2, 
     "control_name": "ion-select", 
     "label": "Select Parent node", 
     "type": "select", 
     "placeholder": "Select parent node", 
     "name": "E2selectoption_parent_node", 
     "readonly": "True", 
     "required": "required", 
     "maxlength": "", 
     "minlength": "", 
     "is_dependent": "true", 
     "masters": "gis_node", 
     "value": ""   
    }, 
    { 
     "sort_order": 3, 
     "control_name": "ion-select", 
     "label": "Select child node", 
     "type": "select", 
     "placeholder": "Select child node", 
     "name": "E3selectoption_child_node", 
     "readonly": "True", 
     "required": "required", 
     "maxlength": "", 
     "minlength": "", 
     "is_dependent": "true", 
     "masters": "gis_node", 
     "value": ""   
    }, 
    { 
     "sort_order": 4, 
     "control_name": "button", 
     "label": "submit", 
     "type": "submit", 
     "placeholder": "", 
     "name": "E4submit_btnsubmit", 
     "readonly": "False", 
     "required": "required", 
     "maxlength": "", 
     "minlength": "", 
     "is_dependent": "false", 
     "masters": "", 
     "value": "" 
    } 
] 
} 
在給定的響應是JSON格式

。 ,所以你可以很容易地綁定這個數據與我們的HTML

相關問題