我有一個組件和一個子組件。子組件處理一些數據並將數據發送給父組件。angularjs綁定的有效方式 - 整個對象與單個值
所以我的流程是這樣的:
component('parentComponent', {
controller: function() {
vm.onSubcomponentValueChange = function (obj) {
// convert data into some other format, lets say for example
vm.objAPIFormat = [];
for (value in obj.somevalue1) {
vm.objAPIFormat.push({
'somevalue1': value
'somevalue2': obj.somevalue2,
'somevalue3': obj.somevalue3
});
}
}
vm.onFormSubmit = function() {
// make an API call with some complex format of obj
// API call in service with vm.objAPIFormat
}
}
}
component('subComponent', {
bindings: {
inputObject: '<',
onInputObjectChange: '&'
}
controller: function() {
vm.$onChange = function (changes) {
if (changes.inputObject) {
vm.obj = changes.inputObject.currentValue;
}
}
//some complex logic with vm.obj.somevalue1 = []
//some complex logic with vm.obj.somevalue2 = ''
//some complex logic with vm.obj.somevalue3 = ''
vm.onSubcomponentChange= function() {
vm.onInputObjectChange(vm.obj);
}
}
}
現在的問題是,這是一種有效的方法?
將inputObject作爲單個對象傳遞給子組件? 或將對象拆分爲單獨的綁定?像:
bindings: {
somevalue1: '<',
somevalue2: '<',
somevalue3: '<'
onSomeValue1Change: '&'
onSomeValue2Change: '&'
onSomeValue3Change: '&'
}