use*_*er7 4 html anchor href angularjs
我必须显示一个<a>标签。但是根据值是否存在,我需要设置href.
这就是我所拥有的:
<a ng-show="source.element!=0" "href="#/resource/{{source.a}}/{{source.b}}/val">
{{source.element}})
</a>
<a ng-show="source.element==0" "href="">{{source.element}}</a>
Run Code Online (Sandbox Code Playgroud)
如果source.element是 0 ,那么点击source.element ( href="")的值时不会发生任何事情
否则,页面必须根据 href 重定向。
有没有更好的方法来做到这一点,因为这会重复代码?
谢谢..
在范围内创建方法
$scope.getUrl = function(source){
return source.element==0 ? '#' : '#/resource/'+source.a+'/'+source.b+'/val';
}
Run Code Online (Sandbox Code Playgroud)
然后从视图调用
<a ng-href="{{getUrl(source)}}">
{{source.element}})
</a>
Run Code Online (Sandbox Code Playgroud)
对于角度标记,最好使用ngHref.
因为如果用户在角度加载之前点击 href ,它会去错误的地址。
| 归档时间: |
|
| 查看次数: |
5871 次 |
| 最近记录: |