Using ng-attr-href from angular to direct user int

2019-09-19 09:51发布

So what I am trying to do is for an icon (link), the user will have already logged on and there is a property identified that is true or false. Based on whether that property is true or false, when the user clicks that icon (link), it will take them to one of 2 pages. So looking at different directives within angular, thought I could use ng-switch, but that doesn't seem viable so I thought ng-attr-href could do it. Trying something like (this is pseudo code):

<a href="somepage.html" ng-attr-href="object.value: false | goto: "otherpage.html""><span></span></a>

So this is like an if/else where when user clicks icon and that value true, they go one place, but if false, they go elsewhere. My difficulty is structuring that and how/where the else goes.

Is there a better way to do this than I am thinking based on what I shared?

Thanks much.

Adding to, where could this be wrong?:

                    <a ng-href="{{ ppt.Globals.value == 'false' '#/claimEnter' : '#/clearSwipe' }}">
                        <img src="ppt/assets/toolIcons/submiticon.svg" >
                        <p>Submit a Claim for Reimbursement</p>
                    </a>

2条回答
疯言疯语
2楼-- · 2019-09-19 10:40

Put a function inside your controller to provide the value, then call it inside the ng-href. This type of decision of which link to go is the controller's responsibility.

ng-href="whereToGo(object.value)"
查看更多
太酷不给撩
3楼-- · 2019-09-19 10:47

Can use a ternary within a {{}} expression in ng-href

<a ng-href="{{object.value ? 'somepage.html' : 'otherpage.html'}}"> 
查看更多
登录 后发表回答