Angularjs 使用 ui-sref 添加 html 到变量 link

Angularjs adding html to variable with ui-sref link

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

ui-sref link 在动态插入时发挥作用的最佳方法是什么?

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() (doc here)
  • ng-href (doc here)

(但仅以防万一,如果传递的参数是 url 的一部分)

这就是结果

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

现在 (在 the plunker) 我们可以将 myStateName 更改为 parent, parent.child, home 并且它会正确地改变生成的 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