Conditionally add target=“_blank” to links with Angular JS

后端 未结 4 2119
别跟我提以往
别跟我提以往 2020-12-05 23:18

I am building a navigation tree in Angular JS. Most links in the tree will point to pages within my website, but some may point to external sites.

If the href of a l

4条回答
  •  死守一世寂寞
    2020-12-05 23:35

    Update

    Or use a directive:

    module.directive('myTarget', function () {
        return {
            restrict: 'A',
            link: function(scope, element, attrs) {
              var href = element.href;
              if(true) {  // replace with your condition
                element.attr("target", "_blank");
              }
            }
        };
    });
    

    Usage:

    Link
    

    When you don't need to use this with Angular routing you can simply use this:

    Link
    

提交回复
热议问题