添加NG-元素动态 [英] Adding an ng-element dynamically

查看:217
本文介绍了添加NG-元素动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想要一个动态创建的NG-的href添加到一个元素我怎样才能得到它像一个平常NG-HREF?

If I wanted to add a dynamically created ng-href to an element how can I get it to behave like a usual ng-href?

<a test-href="/link">Test link</a>

指令:

app.directive("testHref", function() {
  return {
    link: function(scope, elem, attr) {
      var newlink = attr.dbHref + "/additional/parameters";
      attr.$set("ng-href", newlink);
    }
  };
});

这产生&LT;一个测试HREF =/链接NG-HREF =/链接/附加/参数&GT;测试环节和LT; / A&GT; ,但我怎么也得到了NG-HREF的行为,因为它应该?

This produces <a test-href="/link" ng-href="/link/additional/parameters">Test link</a>, but how can I also get the ng-href to behave as it should?

推荐答案

http://jsfiddle.net/UnpSH/

app.directive("testHref", function($compile) {
  return {
    link: function(scope, elem, attr) {
      var newlink = attr.dbHref + "/additional/parameters";
      attr.$set("ng-href", newlink);
      elem.removeAttr('test-href'); // prevent recursion
      $compile(elem)(scope); // compile it again
    }
  };
});

不知道你想要什么来实现,但你应该使用类似 NG-HREF =/路径/ {{scopeVariable}}动态改变你的链接。

这篇关于添加NG-元素动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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