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

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

So I have to watch from, to and dimension separately (I want to use datepicker for the other fields)?
@Marko You can watch multiple scope variables like this answer shows.

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.