2015-09-07 39 views
5

Tôi đang cố gắng loại bỏ href hoặc các thuộc tính ng-href khỏi một góc (v1.2.28) Chỉ thịXóa href với chỉ thị góc

hoạt động tốt khi url không có nội suy chuỗi trong đó.

Bạn có thể giúp tôi tìm ra điều này không?

Tôi chỉ cần thực hiện một jsfiddle đây http://jsfiddle.net/gfvewv5u/1/

angular.module('ui.directives', []); 
angular.module('ui', ['ui.directives']); 

angular.module('ui.directives', []).directive('uiTool', 
    function() { 
    return { 
    restrict: 'EAC', 
    require: '?ngModel', 
    link: function($scope, element, attrs, controller) { 
     var controllerOptions, options; 
     attrs.$set('href', null); 
     element.removeAttr('href'); 
     element.text('iamfoo for what'); 
    } 
    }; 
}); 

angular.module('myApp', ['ui.directives']) 
    .controller('testCtrl', function($scope){ 
    $scope.val = 1; 
    }); 

Và HTML

<div ng-app="myApp"> 
    <div ng-controller="testCtrl"> 
     <a ui-tool href="/test/ts/{{val}}" >Link need to be removed</a> 
     <a ui-tool href="/test/ts" >Link remove</a> 
    </div> 
</div> 

Dựa trên ví dụ của tôi mắt xích đầu tiên vẫn có những href trong khi liên kết 2 doens't

+1

Bạn đã xác định các mô-đun angular.module góc ('ui.directives', []) ;. Vì vậy, loại bỏ [] từ angular.module ('ui.directives', []) chỉ thị ('uiTool', – intekhab

Trả lời

4

Bạn cần phải phá hủy phạm vi trên phần tử có giá trị ràng buộc. Angular theo dõi các ràng buộc bên trong, và sẽ thiết lập lại href.

element.scope().$destroy(); 

như vậy:

link: function($scope, element, attrs, controller) { 
    element.scope().$destroy(); 
    element.removeAttr('href'); 
    element.text('iamfoo for what'); 
} 

cập nhật fiddle

+0

Cảm ơn bạn! Hoạt động thực sự tốt –

+0

Nếu tôi phá hủy phạm vi ràng buộc sẽ không hoạt động, amiright? –

+0

Hi @BrunoSantos nó loại bỏ các ràng buộc nếu đó là những gì bạn có nghĩa là, nhưng đó là điểm của câu hỏi – Jorg

Các vấn đề liên quan