Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS directive : compile template and watch scope

I writing a pretty complex application on Angularjs. This is already big enough to confuse me. I research Angular deeper and I see my code is bad. I understand this concept:

 module.directive('createControl', function($compile, $timeout){
     scope: {
             // scope bindings with '=' & '@'
            },                                                                                                                  
     template: '<div>Template string with binded {{ variables }}</div>',          
     link: function(scope, element, attrs){
            // Function with logic. Should watch scope.
            }

I have several problems:

  • My template is complicated, I have part of template which going in the link function dynamically
  • I need to append compiled template to the element, not to replace.
  • With concept above my template are appended without any interpolation...

So my code is looking like that in simplified view:

module.directive('createControl', function($compile, $timeout){
     scope: {
              var1: '@var1',
              var2: '@var2',
              var3: '@var3'
            },                                                                                                                  
     template: '<div>{{ var1 }} {{ var3 }}</div>',          
     link: function(scope, element, attrs){
              $('.someelement').on('event', function(){
                var2 = 'SNIPPET';  // Need to watch it
              }); 
              var3 = '<span>{{ var2 }}</span>';
            }
     })

My questions is:

How to compile my template with scope variables?

How to watch scope variables?

Should I split my directive for two? If I should, how to do it in right way?

like image 598
I159 Avatar asked May 04 '13 05:05

I159


People also ask

What are the directive scopes in AngularJS?

The directive scope uses prefixes to achieve that. Using prefixes helps establish a two-way or one-way binding between parent and directive scopes, and also make calls to parent scope methods. To access any data in the parent scope requires passing the data at two places – the directive scope and the directive tag.

What is Prelink and Postlink in AngularJS?

Pre-linking function Executed before the child elements are linked. Not safe to do DOM transformation since the compiler linking function will fail to locate the correct elements for linking. Post-linking function Executed after the child elements are linked.

What does $compile do in AngularJS?

The $compile service is the service to use for compilation. Invoking $compile against markup will produce a function you can use to bind the markup against a particular scope (what Angular calls a linking function). After linking, you'll have DOM elements you can place into the browser.

What is Transclude in AngularJS directive?

The ng-transclude directive facilitates AngularJS to capture everything that is put inside the directive in the markup and use it somewhere in the directive's template. Syntax: <ng-transclude.


1 Answers

Angular 1.1.4 released in last few weeks added ability for template to access attributes in directives:

Example:

app.directive('mytest',function(){
  return {
    restrict:'E',    
   template:function( elem,attrs ){      
      return '<div>'+attrs.a+' '+attrs.b+'</div>';
    }
  }
});
<mytest a="Hello" b="World"></mytest>

DEMO

See directive docs for version 1.1.4

like image 138
charlietfl Avatar answered Sep 22 '22 08:09

charlietfl