Dish soap + vinegar and warm water can also be used to trap them. Fill a bowl\dish with some warm water, vinegar or apple cider vinegar and a bit of dish soap. There are variations on the type of container used (bowl, 2 liter bottle, shallow dish, etc.) and I don't know if there is any other trick to it, or what soap works best... handful of times I have tried to do this I only managed to catch a single gnat (or fruit fly), but I've seen it done.
If you're growing in soil I would recommend Mosquito Bits\Dunks or Gnatrol (Bacillus T. i. and toxin), which is also effective against fungus gnat larva. Bti attacks fungus gnats at the larval stage and kills them before they can pupate. In order to control fungus gnats, esp. a more severe infestation, you need to kill either the eggs or larva as well as the adults. Flying adults are quite small and just a few of them can potentially lay hundreds of eggs.
Bti is an endospore forming, facultatively anaerobic bacterium which produces Cry proteins that are toxic and damaging to the gut\organs of a handful of insect species larva. It is a biological control, it wont hurt any other organisms in the soil. It is also very commonly available, and quite effective.