2015-09-07 10 views
5

Próbuję usunąć atrybuty href lub NG-href z kątowym (v1.2.28) dyrektywyUsuwanie href z kątowym dyrektywy

to działa dobrze, gdy adres URL nie ma w nim ciąg interpolacji.

Czy możesz mi pomóc rozwiązać ten problem?

właśnie popełnił jsfiddle tutaj 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; 
    }); 

i 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> 

oparciu o moim przykładzie pierwszym ogniwem wciąż mam href podczas 2nd Link doens't

+1

Już określone kątową moduł angular.module ('ui.directives', []) ;. Tak więc usuń [] z angular.module ("ui.directives", []). Dyrektywa ("uiTool", – intekhab

Odpowiedz

4

Musisz zniszczyć zasięg elementu, który ma przypisaną wartość. Angular śledzi wewnętrznie te wiązania i zresetuje href.

element.scope().$destroy(); 

tak:

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

aktualizowane fiddle

+0

Dziękuję! Działa naprawdę dobrze –

+0

Jeśli zniszczę wiązkę zakresu, nie zadziała, amiright? –

+0

Witam @BrunoSantos usuwa powiązanie, jeśli o to ci chodzi, ale na tym właśnie polegało pytanie – Jorg