2013-10-08 37 views
0

我想輸入表單。 我有選擇選項。根據選定的選項,選擇其中一個選項後,我需要填寫其他三個字段。Angularjs。如何使用選擇框連接窗體中的字段。更改選擇應該改變字段

例如:

如果用戶選擇選項1 3其它字段應該有文本1,1_1,1_2,1_3在其中。 如果用戶選擇Option2,則其他字段中應包含文本2_1,2_2,2_3。

這樣做的最好方法是什麼?

我發現這種方式:

範圍

$scope.select_options = [ 
    {"name": "Option1", 
     "field1": "1_1", 
     "field2": "1_2", 
     "field3": "1_3"}, 
    {"name": "Option2", 
     "field1": "2_1", 
     "field2": "2_2", 
     "field3": "2_3"}, 
    {"name": "Option3", 
     "field1": "3_1", 
     "field2": "3_2", 
     "field3": "3_3"} 
]; 

創建變量,這是HTML代碼

<select ng-model="opt_select" 
     ng-options="option as option.name for option in select_options" 
     ng-init="opt_select = opt_select || select_options[0]" id="well" 
     name="company" class="input"></select> 
<input id="field1" name="field1" placeholder="" class="input" type="text" 
     ng-model="opt_select.field1"> 
<input id="field3" name="field2" placeholder="" class="input" type="text" 
     ng-model="opt_select.field2"> 
<input id="field3" name="field3" placeholder="" class="input" type="text" 
     ng-model="opt_select.field3"> 

它是正確的嗎?

回答

1

是的。 這裏是它的小提琴:Fiddle

不過,當然,因爲它綁定到你的看法雙向,您field的你,如果用戶編輯輸入框select_options將被保存。也就是說,如果用戶在輸入框中將Option3field1更改爲3_1_new,則當用戶選擇另一個選項並選擇Option3時,它仍將是3_1_new。如果這不適合你的用例,你必須考慮其他的東西。

除此之外,對於最佳做法和其他代碼審查,您應張貼到https://codereview.stackexchange.com/

相關問題