eat-sleep-code
eat-sleep-code

Reputation: 4855

AngularJS Different URL If

I currently have the following link on my page.

<a href="/#!/blog/{{article.id}}" title="Permalink to {{article.title.__cdata}}" itemprop="url">{{article.title.__cdata}}</a> 

I would like to make a small change, but am not sure of the proper syntax.

If {{article.url}} is NOT zero-length, I would like to use that instead of /#!/blog/{{article.id}}

If {{article.url}} is zero-length, I would like to continue to use /#!/blog/{{article.id}}

Upvotes: 0

Views: 22

Answers (1)

JB Nizet
JB Nizet

Reputation: 691735

<a ng-href="{{ article.url || '/#!/blog/' + article.id }}" ...>

or you could put this logic in the controller if you find it too complex to be in the view:

<a ng-href="{{ getPermalink(article) }}" ...>

$scope.getPermalink = function(article) {
    return article.url || '#!/blog/' + article.id;
}

Upvotes: 1

Related Questions