Search code examples
javascriptangularjsodataconcatenation

Issues concatenating strings for OData filter using AngularJS


I am trying to create an OData filter in AngularJS that gets the variables passed from my view.

I am struggling with the concatenation. Especially with the start and end quotes ". I researched and tried various options like adding \ etc., but no luck.

The hard-coded filter would look like this:

$filter: "contains(ProductCode, 'GDN') and Price ge 5 and Price le 20"

Here is my filter using variables passed from the view:

var newFilter = "contains(ProductCode, " + "'" + vm.codeO + "')" + " and Price ge " + vm.priceGtO + " and Price le " + vm.priceLtO "\""

My filter using the variable must look exactly the same. Including the start and end quotes ".

Thank you in advance!


Solution

  • You need to remove the ending "\"".

    The string that you want to output is foo and not foo".

    My filter using the variable must look exactly the same. Including the start and end quotes (").

    That's your error. The start and end quotes were used just to say that the inner content is a string. You don't need to add extra quotes if you already have a string variable.

    Also, you've added some additional concatenations that were not necessary. Try this:

    var newFilter = "contains(ProductCode, '" + vm.codeO + "') and Price ge " + vm.priceGtO + " and Price le " + vm.priceLtO