1

I am new to AngularJS and trying to see if this is possible. I have looked at a lot of $http.get questions online but can't find an answer to this.

I have an api call that has three different parameter key possibilities.

$http.get(url, {params : {$scope.searchBy : $scope.value}})

$scope.searchBy is marked as a syntax error. It has 3 possible values from a dropdown list.

I tried var search = $scope.searchBy; but when I use {params : {search : $scope.value}} the value is "search" and not $scope.searchBy.

Any suggestions how this can be done without if statements and separate api calls?

Thank you.

1
  • What call are you trying to achieve at the end ? url?searchBy=... ? Commented Nov 16, 2017 at 19:30

2 Answers 2

1

You will have to do something like this as this is valid javascript syntax.

var paramValues = {};
paramValues[$scope.searchBy] = $scope.value;
return $http.get(url, {params: paramValues});

When you define an object using the syntax you have in your question the property/field name must be a known constant. If it is variable, which you want in this case, then you can create the object first and then set the value using the obj[propertyName] = someValue; notation.

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

1 Comment

@malka - glad to help.
1

Use property accessor bracket notation to construct the desired params object:

var config = { params: {} };
config.params[$scope.searchBy] = $scope.value;

$http.get(url, config);

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.