Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS remove attributes

I have a directive that replaces my custom tag with some regular HTML. There are some attributes that I'd like to remove. For example, given the syntax

<ui mybutton width="30"></mybutton>

my directive transforms it into

<div width="30" class="btn">bla bla </div>

I want to remove that "width=30" and add style="width:{{old width value here}}"

I've been experimenting with the compile and link function. Should I do that in the compile or in the link function?

I thought I had to do it in the compile function because I want to make a modification in the template.

See it live in http://jsfiddle.net/WptGC/2/ WARNING: your browser might hang! See it live and safely http://jsfiddle.net/WptGC/3/ the code that makes everything crash is commented.

.directive('mybutton', function($compile) {
return {
    restrict: 'A',
    //transclude: true,
    template: '<div class="this is my subscreen div" style="width:{{width}}"></div>',
    replace: false,
    /*scope: {
        width: '@',
        height: '@',
        x: '@',
        y: '@'
    },*/

    compile: function($tElement, $tAttrs) {
        console.log("subscreen template attrs:");
        console.log($tAttrs);
        var el = $tElement[0];
         //el.getAttribute('width');
        var stylewidth = el.getAttribute('width'); 
        el.removeAttribute('width');

         return function(scope) {
            $compile(el)(scope);
         }
    }
}
})

I'm just getting a strange loop (that console.log shows up a few thousand times)

like image 247
Eduard Gamonal Avatar asked Apr 11 '13 13:04

Eduard Gamonal


People also ask

How to remove div in AngularJS?

Approach: Here first we select the element that we want to remove. Then we use remove() method to remove that particular element. Example 1: Here the element of class('p') has been removed.

How to remove HTML tags from string in AngularJS?

Show activity on this post. /* 'stripHTML': returns the concatenated text nodes of the html string provided */ MyApp. filter ('stripHTML', [function () { return function (stringWithHtml) { var strippedText = $('<div/>'). html(stringWithHtml).

How to set attribute dynamically in AngularJS?

var table = document. getElementById("div_id"). setAttribute("ng-click", "function_name()"); $scope. $apply();

How to set attribute in AngularJS directive?

directive('sharedAsset', function (globalVars) { return { restrict: "A", scope: { attr: "@attr" }, link: function (scope, element, attrs) { var fullPath = globalVars. staticWebsite + "/app/styles/main/" + attrs. sharedAsset + "? build=" + globalVars.


1 Answers

Unless I'm missing some other requirement you should just be able to use isolate scope and a template like:

http://jsfiddle.net/WptGC/6/

app.directive('backbutton',function(){
  return {
    restrict: 'A',
    replace:true,
    scope: {
        x: '@',
        y: '@'
    },
    template:'<button style="width: {{x}}px; height: {{y}}px">A back-button template</button>',
    link: function (scope, element, attrs) {
      element.removeAttr('x').removeAttr('y');
    }
  }
});
like image 180
Langdon Avatar answered Oct 14 '22 13:10

Langdon