0

I want to build a url where parts of the url string are dynamic build with a select box.

The watch is only called at startup:

$scope.$watch('url', function () {
    $scope.buildUrl();
}, true);

Here is the fiddle:

http://jsfiddle.net/mkeuschn/wJGFm/

best regards

Marko
  • 1,291
  • 1
  • 21
  • 37

1 Answers1

1

The $watch will only fire if the value being watched changes. In this case, it is better to watch dimension, since this is the selection that is changing. Then, you can re-assign the dimension part of the url and re-build.

Here is an updated fiddle.

JS:

$scope.buildUrl = function () {
    $scope.url.dimension = $scope.dimension.value;
    $scope.completeUrl = $scope.url.base + "dateFrom=" + $scope.url.from + "&dateTo=" + $scope.url.to + "&dimension=" + $scope.url.dimension;
};

$scope.$watch('dimension', function () {
    $scope.buildUrl();
}, true);
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
  • So I have to watch from, to and dimension separately (I want to use datepicker for the other fields)? – Marko Jun 02 '14 at 12:58
  • @Marko You can watch multiple scope variables [like this answer shows](http://stackoverflow.com/a/23495281/269608). – Austin Thompson Jun 02 '14 at 14:05