Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS directive link function not being called

I am trying to use angular-http-auth library with bootstrap modal window. Modals are working fine but I have problems with directives. Here is a jsfiddle link - http://jsfiddle.net/jCUSh/85/ . I am trying to add a directive that will be invoked and that adds listeners. I have simplified the example in jsfiddle, so you won't see http-auth imports. However scope.on('') elements are still left (they don't break the picture anyway).

My question is - why isn't the linking function called? I added elem.addClass('test') as an example. I believe the solution is super simple, just unable to see it.

Also less important question - is it ok to pass scope as a parameter to another scope? I need it to close the modal window.

Thanks

like image 581
Ruzard Avatar asked Dec 24 '13 15:12

Ruzard


People also ask

How do I invoke a directive in AngularJS?

In addition to all the built-in AngularJS directives, you can create your own directives. New directives are created by using the . directive function. To invoke the new directive, make an HTML element with the same tag name as the new directive.

What is Link function in AngularJS directive?

link function is basically used to manipulate the DOM( Document Object Model ) element using custom directive. link option in custom directive registers DOM listener and also update the DOM. Watch the live demo or download code from the link given below.

What does $compile do in AngularJS?

Overview. Compiles an HTML string or DOM into a template and produces a template function, which can then be used to link scope and the template together. The compilation is a process of walking the DOM tree and matching DOM elements to directives.

What is the difference between controller and link in directives?

In my opinion, we use controller when we need to share or reuse $scope data or we want directive interactive with each other. link is used for DOM manipulation tasks. This article compare the difference of controller and link in AngularJS directive with example, you could refer to it.


2 Answers

Most of directive errors are displayed in the console, just enable logging:

app.config(function($logProvider){
    $logProvider.debugEnabled(true);
});

Additionally, you may assert if directive was actually loaded:

angular.module('my', [])
    .controller('Controller', [ '$scope', '$injector', 
        function ($scope, $injector) {
           assertDirectives($injector, [ 'dir1', 'dir2']);
         });

function assertDirectives($injector, directives){
    _.each(directives, function(directiveCamelCase){
        if( !$injector.has(directiveCamelCase + 'Directive') ) 
             throw("Directive " + directiveCamelCase + " is not available.")
    });
}
//you may replace underscore's `each` with jquery `each` or regular js loop

Thus you'd have no need to guess why directive is't working.

like image 156
setec Avatar answered Sep 19 '22 14:09

setec


Two things are at play here..

One is you must pass the directive through the class attribute and not the ng-class

Secondly, the "C" character you pass to the restrict property is a character with ASCII of 1057 (not our usual ASCII 67 char)

Fixed demo at http://jsfiddle.net/gaby/jCUSh/87/

like image 20
Gabriele Petrioli Avatar answered Sep 18 '22 14:09

Gabriele Petrioli