0

I have this code in my directive

   compile: function compile (tElement, tAttributes, transcludeFn) {

        if (tAttributes.drag == 'false') {
            tElement.find('.myclass').removeAttr('draggable');
        }

        //attrs.$set('ngModel', 'new value');
        return {
            pre: function preLink (scope, element, attributes, controller, transcludeFn) {
                // Pre-link code goes here
            },
            post: function postLink (scope, element, attributes, controller, transcludeFn) {

This works fine

But i want to add attribute instead of remove attribute based on boolean like this

        if (tAttributes.drag == 'true') {
            tElement.find('.myclass').attr('draggable');
        }

But this is not working.

I thing i need to recompile element after adding but i don't know how to do it

7
  • If dragable is directive then look at my answer how to add directive from within directive... stackoverflow.com/a/28622263/1505865 Commented Feb 23, 2015 at 4:42
  • $(tElement).find('.myclass').attr('draggable'); ???? Commented Feb 23, 2015 at 4:43
  • @JenishRabadiya i tried someting like that before but i was not sure where to put in pre link or post link . also i get error $compile is not a function Commented Feb 23, 2015 at 4:47
  • include $compile to the directive function like I have done. No need to prelink and postlink but don't try to compile element itself instead get children and compile it and then replace that children. I am considering you are having element directive. Commented Feb 23, 2015 at 4:51
  • @JenishRabadiya I did include that $compile function and i used that i used inside postLink function and i get same error again Commented Feb 23, 2015 at 4:58

2 Answers 2

1

Try adding the attribute in template function of the directive definition.

module.run(function ($templateCache, $http) {
    $http.get('__templateURL__')
      .then(function (response){
        $templateCache.put('__templateID', response.data)
      })
  });

module.directive('x', function ($templateCache) {
    return {
      template: function (tEl, tAttrs) {
        var template = $($templateCache.get('__templateID')); 
        if (tAttrs.drag == 'true') {
          template.find('.myclass').attr('draggable');
        }
        return template[0].outerHTML;
      }
    }
  });
Sign up to request clarification or add additional context in comments.

5 Comments

can i pull template from file and then chnage its attribute. i can't write the whole html as string
Try this... module.run(function ($templateCache, $http) { $http.get('__tempalteURL__') .then(function (response) { $templateCache.put('__templateID__', response.data); }) }) .directive('x', function ($templateCache) { return { template: function () { var el = $templateCache.get('__templateID__'); //add the attribute } } })
can you write that in above answer , its very hard to understand that in comment
thanks for that , that will work if my template is simple html. my problem is template itself conatin another driective , so at ttime template is not yet compiled into full html so that i can add some attribute. any idea to fix that
That should not be a problem. If you still face the problem.. please create a plunker/fiddle for the same.
0

jQlite (angular's "jQuery" port) doesn't support lookup by classes.

find() - Limited to lookups by tag name

So you should try querySelector

if (tAttributes.drag == 'true') {
    tElement[0].querySelector('.myclass').attr('draggable');
}

6 Comments

But that removing of attribute is working fine with the code i provided. its only that adding attribute is not wokring because i think the dircetive is already compiled when i add the directive so it dont take into effect
are you wrapped jquery over angular ?
what u mean by wrapping jquery
are you included jquery before angular.js ?
That's why you can use find method here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.