2015-12-14 26 views
1

我使用有角度的formly創建窗體。我已經動態地添加了使用有角度的形式重複節的添加按鈕上的選擇字段和文本區域。Angular Formly - 如果在前一個選擇字段中已經選擇了選項,則選擇字段應該會出錯

要求 - 從多個選擇字段中,每個選擇字段都應該選擇唯一的選項。

像「貨幣已經選擇了」

Controller.js

$scope.formFields = [ { 
      type: 'repeatSection', 
      key: 'details', 
      templateOptions: { 
      btnText:'Add ', 
      fields: [ 
       { 
       className: 'row', 
       fieldGroup: [ 
         { 
         type: 'select', 
         key: 'currency', 
         wrapper: 'loading', 
        templateOptions: { 
          label: $translate.instant('CURRENCY'), 
          valueProp: "value", 
          labelProp: "name", 
          options: [ 
           {name: "Rupee", value: "INR"}, 
           {name: "Doller", value: "$"}, 
           {name: "Pound", value: "Pound"} 
           ] , 
          required: true, 
          placeholder: $translate.instant('SELECT TYPE FROM LIST'), 
         } 
         },   
        { 
        type: 'textarea', 
        key: 'debitNote', 
        templateOptions: 
        { 
         label: $translate.instant('DEBIT NOTE'), 
         rows: 4 
        } 
        } 
       ] 
       } 
      ] 
      } 

    } 
]; 

回答

1

選項1如何檢查是否選擇在先前所選字段使用formly角度,並顯示錯誤消息已經選擇與否:自定義驗證

製作一個自定義的驗證,如:

vm.customValidator = { 
    expression: function(viewValue, modelValue, scope) { 
    if(scope.model.currencies){ 
     angular.forEach(scope.model.currencies, function(val, key) { 
     if(val === modelValue) { 
      console.log('Currency already selected! '+val+' === '+modelValue); 
      return false; 
     } 
     }); 
    } 
    return true; 
    }, 
    message: 'Currency already selected' 
}; 

示例:http://jsbin.com/nopike/1/edit?html,js,console,output

*備註由於我匆忙,驗證消息未顯示。請參考以下鏈接:

選項2:多選

使用多選:

+0

謝謝benoror!這個答案真的幫我解決了這個問題。 – Abhijeet

相關問題