11

I am new to AngularJS and trying to build an AngularJS practice app, in which, the user will concatenate a url from multiple inputs, i.e. protocol, domain, path, param1, param2, param3... etc.

And the app will create a link to the url:

<a>{{protocol}}://{{domain}}{{path}}?{{param1}}&{{param2}}&{{param3}}</a>

Above url is used twice. Once on the href attribute, as well as the actual text. Now what I want to do is something like:

<a href="{{url}}">{{url}}</a>

But I am not sure where to assign url. I tried below, and it worked, but doesn't seem correct.

<a href='{{url = protocol+"://"+domain+path+"?"+param1+"&"+param2+"&"+param3}}'>{{url}}</a>

Assuming that url is used many times in the app, where would be the most appropriate place to assign url?

5
  • try using ng-init docs.angularjs.org/api/ng.directive:ngInit Also, you might wanna use ng-href instead of href. Commented Aug 12, 2013 at 9:53
  • 1
    Hello, can't you store it inside your scope with $scope.url = concanated url; and then user {{url}} inside your controller? Commented Aug 12, 2013 at 9:56
  • Hi @askkirati, but if I do that, when user changes one of the variables, url didn't get updated... $scope.url = concatenated url seemed to only happen once during init. Did I do something wrong? Commented Aug 12, 2013 at 10:06
  • @AdityaJain, samething, ng-init only does it once... Commented Aug 12, 2013 at 10:12
  • agreed, I think best solution is what @mikel provided. Commented Aug 12, 2013 at 10:13

4 Answers 4

24

You shouldn't assign scope variables in the view (the HTML) - it's only for reading them.

To make a new variable from inputs, add a ng-model to each of them and then in the controller define a method that makes a $scope variable from them e.g.

Your HTML form:

<div ng-controller="MyCtrl">

    <input type="text" ng-model="urlParts.protocol">
    <input type="text" ng-model="urlParts.domain">
    <!-- etc -->
    <a ng-href="{{makeUrl()}}">{{makeUrl()}}</a>
</div>

JS:

function MyCtrl($scope) {
    $scope.urlParts = {};
    $scope.urlParts.protocol = ""; 
    $scope.urlParts.domain = ""; 
    // etc... 
    // These values will be updated when the user types in the input boxes

    $scope.makeUrl = function() {
      return $scope.urlParts.protocol + "://" + $scope.urlParts.domain + $scope.urlParts.path + "?" + $scope.urlParts.param1 + "&" + $scope.urlParts.param2 + "&" + $scope.urlParts.param3;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why have you used ng-href instead of regular href?
Duh! that's so simple. Thank you so much!
1

Another similar approach is to use ng-init directive to create a dynamic property which represents the concatenation of all those properties. You can call the function or just concatenate inline.

 <a ng-init="urlParts.url=makeUrl()" 
ng-href="urlParts.url">{{urlParts.url}}
</a>

<a ng-init="urlParts.url=urlParts.protocol + '://' + $scope.urlParts.domain..." 
    ng-href="urlParts.url">{{urlParts.url}}
    </a>

Reference: http://www.ozkary.com/2015/03/angularjs-ng-model-concatenate-model.html

Try It: http://plnkr.co/edit/PSvwCE?p=info

Comments

0

If you want to bind a dynamic url with href then you can manipulate you URL in ng-click or ng-mousedown event and bind to target element.

JS:

var goToLinkedPage = function (event, basePath, param1, param2, param 3) {
    var newState = basePath + "?" + param1 + "&" + param2 + "&" + param3;
    jQuery(event.target).attr('href',newState);
};

HTML:

<a ng-mousedown="goToLinkedPage($event, basePath, param1, param2, param3)"> Click Here </a>

Comments

0

Just use this binding expression to concatenation of more than two scope variables in HTML.

{{UserData.FirstName + ' ' + UserData.MiddleName + ' ' + UserData.LastName}}

If you want to do this inside any attribute, Just write like this:

<input type="text" ng-value="UserData.FirstName + ' ' + UserData.MiddleName + ' ' + UserData.LastName">

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.