2017-02-15 28 views
0

我有一個小窗體,有一些複選框的默認值,例如,有一個人模型,當formly生成的窗體中的複選框「狗」被點擊時,它將添加到一個「寵物」數組構成了人體模型的一部分。我怎樣才能以形式實現這一目標?角形formly - 複選框模型值模板

{ 
    key: "dog", 
    type: "checkbox", 
    templateOptions: { 
      ??? 
    } 
} 

形式例如:

Person name: Joe 
Pets: 
[x] dog 

這將設置模式:

{ 
     "name": "Joe" 
     "pets": [ "dog" ] 
} 
+0

請提供有關問題的更多細節。 –

+0

添加更多信息,希望它有幫助。 – David

+0

在該線程中解決了類似的問題:http://stackoverflow.com/q/14514461/2275775 答案很長,很受歡迎且檢查得當,所以創建新的答案並不是最好的主意。 –

回答

0

@veg,這個問題是角formly,沒有棱角。這不一樣。顯着如此。

在初始化:

vm.model = { //initialize a model with a pets property holding an empty array. 
    pets: [] 
} 

在你的領域模型:

{ 
    key: 'dog', 
    type: 'checkbox', 
    templateOptions: { 
     label: 'dog', 
     onChange: function($viewValue, $modelValue, $scope) { 
     if($modelValue) $scope.model.pets.push('dog'); //if it's true--add it 
     else { //need to remove it from the array 
      var index = $scope.model.pets.indexOf('dog'); //get the index 
      $scope.model.pets.slice(index, index+1); //remove it from the array 
     } 
     } 
    }, 
}