1

Currently I am using the code below, and it is working fine. But I want to remove the if statements.

$scope.setValue = function (dataPut,dataRecive) {
    if(dataPut=='profData')
        $scope.formData.profData=dataRecive;    
    else if(dataPut=='cartData')
        $scope.formData.cartData=dataRecive;    
    else if(dataPut=='buyflowData')
        $scope.formData.buyflowData=dataRecive;
}

1 Answer 1

4

You can use Bracket notation property accessors like

$scope.setValue = function(dataPut, dataRecive) {
  $scope.formData[dataPut] = dataRecive;
}

However note: it will create property if it's not defined for $scope.formData .

var formData = {};
var setValue = function(dataPut, dataRecive) {
  formData[dataPut] = dataRecive;
}
var value = prompt("Enter a value");
setValue('value', value);
console.log(formData)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.