I'm still new to angularjs, and I have a problem that I seem to not be able to find solution, and I don't have time to go look into angular source.
This is my scenario:
I have some json data with collection of urls that I want to show on screen.
I have an element with ng-repeat="link in links"
and inside I have
<a ng-href="{{link.url}}">{{link.title}}</a>
That works, but all links are pointing to mydomain/apppath/valueoflink.title I want them to be absolute, only valueoflink.title without any prefix.
How to tell angular that it is absolute not relative url?
If you prefix the URL with // it will be treated as an absolute one. For example: <a href="//google.com">Google</a> . Keep in mind this will use the same protocol the page is being served with (e.g. if your page's URL is https://path/to/page the resulting URL will be https://google.com ).
The ng-href directive overrides the original href attribute of an <a> element. The ng-href directive should be used instead of href if you have AngularJS code inside the href value. The ng-href directive makes sure the link is not broken even if the user clicks the link before AngularJS has evaluated the code.
An absolute URL contains the entire address from the protocol (HTTPS) to the domain name (www.example.com) and includes the location within your website in your folder system (/foldernameA or /foldernameB) names within the URL. Basically, it's the full URL of the page that you link to.
The ng-href directive should be used instead of href if you have AngularJS code inside the href value. The ng-href directive makes sure the link is not broken even if the user clicks the link before AngularJS has evaluated the code. Supported by the <a> element.
HTML Web Development Front End Technology To link pages using absolute URL in HTML, use the <a> tag with href attribute. Absolute URL is used to link an external website.
Why use relative URLs in lieu of absolute URLs? The answer is that relative URLs come in handy if the file path of the linked file changes, such as in the case of a website restructuring or a domain change. If this happens, the relative URL will still work, since it only contains the name of the linked resource and not the entire file path.
The href attribute indicates the destination of the hyperlink. Without the href attribute, the a element won’t work. You’ve already met one instance of href in the above introduction when I linked to the definition of the World Wide Web. The HTML for that hyperlink looks like this:
As Goran said, his solution will work only if all urls are like 'www.google.com'.
If you have a combination of different types of the urls, e.x. 'www.google.com', 'https://github.com', 'http://goo.gl', 'github.com', you can use ng-href with an angular filter:
<a ng-href="{{link.url|myFilter}}">{{link.title}}</a>
and a filter, which will append 'http://' to your url, if it starts with 'www':
'use strict';
myApp.filter("myFilter", function () {
return function (link) {
var result;
var startingUrl = "http://";
var httpsStartingUrl = "https://";
if (link.startWith(startingUrl) || link.startWith(httpsStartingUrl)) {
result = link;
}
else {
result = startingUrl + link;
}
return result;
}
});
String.prototype.startWith = function (str) {
return this.indexOf(str) == 0;
};
I solved it by prefixing data in json with 'http://' to make them trully absolute urls, and angularjs respects that.
Then I understood that angular is actually not doing anything with value, it is just putting it there as it is, and it is my fault to see that.
updating code to this solves problem when all urls are like 'www.google.com'
<a ng-href="http://{{link.url}}">{{link.title}}</a>
Plain old 'inspect element' uncovered issue, and I ignored the fact that ng-href binds from {{value}} syntax, so this is why my first attempt to put ng-href="'http://'+{{value}}"
failed and led me to ask question prematurely, but I'm not sure if I should delete it as I may not be only one making such mistake.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With