aztack aztack - 2 months ago 10
AngularJS Question

How to surround text with tag conditionally in AngularJS?

How to surround text with tag conditionally in AngularJS?
for example:

function Controller($scope){
$scope.showLink = true or false, retrieved from server;
$scope.text = "hello";
$scope.link = "..."
}


if {{showLink}} is false

<div>hello</div>


else

<div><a href="{{link}}">hello</a></div>

Answer

ngSwitch is suitable for that:

<div ng-switch="!!link">
    <a ng-href="{{link}}" ng-switch-when="true">linked</a>
    <span ng-switch-when="false">notlinked</span>
</div>
Comments