如何在没有重定向的情况下使用空的href属性?

时间:2016-08-04 08:30:47

标签: javascript angular angular2-routing

有没有办法让href没有重定向回到angular2中的根目录?因为我有这个<a href="" (click)="modal.open()"></a>代码来打开一个模态但它会将我重定向回我的主页。

我知道因为routeConfig有角度,但有没有办法说绕过它?

{ path: '', component: LoginComponent, pathMatch: 'full'},

请注意,如果完全删除href有效,则可能没有检查w3c验证。

4 个答案:

答案 0 :(得分:11)

您可以使用href="javascript:;"来实现href,而不会产生任何副作用。

答案 1 :(得分:1)

Angular使用ng-href代替href。因此,如果您想要href attr,那么您应该使用ng-href

如果你想根据W3C规范拥有有效的HTML,那么不要使用Angular,因为(click)=...等无效的属性:)恕我直言这个HTML有效性被高估了。

如果您只想打开一个模态窗口,那么请不要使用<a>标记,因为这不是它们的主要标记。 <a>用于导航。请改用<button>。这也将解决你的href。

问题

答案 2 :(得分:1)

@Composable fun StationsScreen(deviceLocation: LocationData, openDrawer: () -> Unit) { var scrollPosition = ScrollPosition(0.px,0.px) FlexColumn { inflexible { TopAppBar( title = {Text(text = "Stations")}, navigationIcon = { VectorImageButton(id = R.drawable.ic_baseline_menu_24) { openDrawer() } } ) } inflexible { Column ( mainAxisSize = LayoutSize.Expand, crossAxisSize = LayoutSize.Expand ){ LocationWidget(deviceLocation) } } inflexible { Column( mainAxisSize = LayoutSize.Expand, crossAxisSize = LayoutSize.Expand ){ PushWidget(){ deviceLocation.lat++ deviceLocation.lng++ } } } inflexible{ Column( mainAxisSize = LayoutSize.Expand, crossAxisSize = LayoutSize.Expand ) { ScrollPosWidget(scrollPosition = scrollPosition) } } flexible(flex = 1f) { VerticalScroller (onScrollPositionChanged = { px: Px, px1: Px -> scrollPosition.posX = px scrollPosition.maxX = px1 }){ Column { for(i in 0..20) { HeightSpacer(16.dp) imageBank.forEach { imageItem: ImageItem -> Text(text = imageItem.title ?: "<Empty>") Divider() } } } } } } } 在单击链接后@Composable fun VerticalScroller( scrollerPosition: ScrollerPosition = +memo { ScrollerPosition() }, onScrollPositionChanged: (position: Px, maxPosition: Px) -> Unit = { position, _ -> scrollerPosition.value = position }, isScrollable: Boolean = true, @Children child: @Composable() () -> Unit ) { Scroller(scrollerPosition, onScrollPositionChanged, true, isScrollable, child) } 不允许使用内联脚本(例如href="javascript:;"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src)时会产生错误。

您可以在点击时使用带href.event.preventDefault()的空href。

Content-Security-Policy

答案 3 :(得分:0)

我认为这个问题的更好答案可能是:

<a href="" (click)='$event.preventDefault();modal.open()'>
相关问题