Angularjs使用ui-sref链接将html添加到变量 [英] Angularjs adding html to variable with ui-sref link

查看:13
本文介绍了Angularjs使用ui-sref链接将html添加到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code that adds html to a variable. However, when it get's shown on the page, the link doesn't work.

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

What's the best way to get a ui-sref link to work when inserting it dynamically?

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>

推荐答案

一个工作插件

我想说,我们可以使用以下组合:

I would say, that we can use combination of:

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

这就是结果

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

现在(在 plunker)myStateName 更改为 parent, parent.child, home 它将正确更改生成的href:

And now (in the plunker) we can change myStateName into parent, parent.child, home and it will change properly the generated href:

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

因为这些是 plunker 中的状态

Because these are states in plunker

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

检查它这里

这篇关于Angularjs使用ui-sref链接将html添加到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆