gpt4 book ai didi

javascript - Angularjs 使用 ui-sref 链接将 html 添加到变量

转载 作者:数据小太阳 更新时间:2023-10-29 05:33:08 25 4
gpt4 key购买 nike

我有以下将 html 添加到变量的代码。但是,当它显示在页面上时,链接不起作用。

动态插入 ui-sref 链接的最佳方法是什么?

JavaScript

.controller('page', function($scope, $rootScope, $http, $state, $sce) {

$scope.message = $sce.trustAsHtml('A <a ui-sref="login">login</a> link');

})

HTML

<div ng-bind-html="message"></div>

最佳答案

a working plunker

我会说,我们可以结合使用:

  • $state.href() (文档 here )
  • ng-href (文档 here )

(但仅在传递的参数是 url 的一部分的情况下)

结果是这样

<a ng-href="{{$state.href(myStateName, myParams)}}">

现在 (在 the plunker 中) 我们可以将 myStateName 更改为 parentparent.childhome 并且它会正确地改变生成的 href:

<input ng-model="myStateName" />
<input ng-model="myParams.param" />

因为这些是 plunker 中的状态

$stateProvider
.state('home', {
url: "/home",
...
})
.state('parent', {
url: "/parent?param",
...
})
.state('parent.child', {
url: "/child",
...

查一下here

关于javascript - Angularjs 使用 ui-sref 链接将 html 添加到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30058287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com