angularjs directive will not call controller function

r3plica

I have seen these issues before and it is usually because of parameters, etc, but I have a very simple example that I can't get to work....

I have a directive:

.directive('imageFileInput', function () {

    return {
        restrict: 'A',
        transclude: true,
        templateUrl: '/assets/tpl/directives/imageFileInput.html',
        scope: {
            onComplete: "&imageFileInput"
        },
        link: function (scope, element, attr) {

            // Get our input
            var input = element.find("input");

            // Function to handle the displaying of previews
            var preview = function () {

                // If we have an input
                if (input) {

                    // Create our file reader
                    var fileReader = new FileReader();

                    // Get our file
                    var file = input[0].files[0];

                    // Read the data from the file
                    fileReader.readAsDataURL(file);

                    // When the data has been read
                    fileReader.onload = function (e) {

                        // Apply the scope
                        scope.$apply(function () {

                            // And attach the result to our scope
                            scope.thumbnail = e.target.result;

                            // If we have a callback function
                            if (scope.onComplete) {

                                // Execute the function
                                scope.onComplete();
                            }
                        });
                    };
                }
            };

            // Bind a function to the onchange event
            input.bind('change', function () {

                // Create our preview
                preview();
            });
        }
    };
});

and the template looks like this:

<div class="thumbnail">
    <input type="file" accept="image/*" />
    <img src="{{ thumbnail }}" ng-if="thumbnail" />
    <div class="caption" ng-transclude>

    </div>
</div>

Pretty simple so far, it is just a directive that creates a nice preview for a file input. So the declaration of this directive is in my view like this:

<div id="{{ panel.id }}" image-file-input="controller.upload()">
    {{ panel.title }}
</div>

and in my controller, I have this function:

.controller('EditKitGraphicsController', ['GarmentService', 'garment', function (garments, garment) {
    var self = this;

    // Get our garment
    self.garment = garment;

    self.upload = function () {

        console.log('uploading');
    };
}])

So, if I break this down:

  • My directive isolates the scope and expects a callback function called onComplete which is passed with the directive declaration (i.e. image-file-input="callback()")
  • My directive calls this function after a scope.$apply and after checking the callback function exists, no parameters are passed
  • My view passes the controller function upload() which again has no parameters
  • My controller has a function attached to the scope called upload, which has a console.log in it
  • Yet nothing gets executed...

Does anyone know why?

New Dev

You have most likely forgotten to specify a "controller as" alias in ng-controller:

<div ng-controller="EditKitGraphicsController as controller">
  <div image-file-input="controller.upload()">
</div>

Also, there is no need to make this check:

if (scope.onComplete){
   scope.onComplete();
}

In fact, scope.onComplete will never be undefined, since Angular creates a function call wrapper, even if the attribute is not assigned. You can safely make the call:

scope.onComplete();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angularjs directive call controller function

From Dev

Angularjs directive call controller function

From Dev

AngularJS How to call directive function from controller

From Dev

Angularjs pass function from Controller to Directive (or call controller function from directive) - with parameters

From Java

Call a controller function from a directive without isolated scope in AngularJS

From Dev

how to call one directive controller function from another one directive using angularjs

From Dev

AngularJS Trigger directive from controller call

From Dev

Angularjs controller function vs directive function

From Dev

AngularJS Controller - Directive -> invokation of Controller function from directive

From Dev

how to call parent function controller from directive react component angularjs + reactjs?

From Dev

need to get template from ng-init function call of controller for the custom directive in Angularjs

From Dev

Call Angular controller function from directive

From Dev

Angular.js call directive function in controller

From Dev

Call controller function from angular directive

From Dev

Call controller function from directive with arguments

From Dev

Call function in controller from a nested directive

From Dev

Is it 'bad practice' for an Angular controller to call a function on a directive?

From Dev

Angular JS - Call a Directive function in an outside Controller

From Dev

How to call a directive function from controller?

From Dev

Call controller function from directive with arguments

From Dev

Directive does not have access to controller function (AngularJS)

From Dev

AngularJS execute controller function in directive template

From Dev

AngularJS: Exeucting function from controller in a directive

From Dev

Passing a callback form directive to controller function in AngularJS

From Dev

AngularJS: Exeucting function from controller in a directive

From Dev

Calling directive function from controller angularjs

From Dev

How to call function in controller in angularjs?

From Dev

Call Controller Function from Directive (with params passed from directive)

From Dev

Call Controller Function from Directive (with params passed from directive)

Related Related

  1. 1

    Angularjs directive call controller function

  2. 2

    Angularjs directive call controller function

  3. 3

    AngularJS How to call directive function from controller

  4. 4

    Angularjs pass function from Controller to Directive (or call controller function from directive) - with parameters

  5. 5

    Call a controller function from a directive without isolated scope in AngularJS

  6. 6

    how to call one directive controller function from another one directive using angularjs

  7. 7

    AngularJS Trigger directive from controller call

  8. 8

    Angularjs controller function vs directive function

  9. 9

    AngularJS Controller - Directive -> invokation of Controller function from directive

  10. 10

    how to call parent function controller from directive react component angularjs + reactjs?

  11. 11

    need to get template from ng-init function call of controller for the custom directive in Angularjs

  12. 12

    Call Angular controller function from directive

  13. 13

    Angular.js call directive function in controller

  14. 14

    Call controller function from angular directive

  15. 15

    Call controller function from directive with arguments

  16. 16

    Call function in controller from a nested directive

  17. 17

    Is it 'bad practice' for an Angular controller to call a function on a directive?

  18. 18

    Angular JS - Call a Directive function in an outside Controller

  19. 19

    How to call a directive function from controller?

  20. 20

    Call controller function from directive with arguments

  21. 21

    Directive does not have access to controller function (AngularJS)

  22. 22

    AngularJS execute controller function in directive template

  23. 23

    AngularJS: Exeucting function from controller in a directive

  24. 24

    Passing a callback form directive to controller function in AngularJS

  25. 25

    AngularJS: Exeucting function from controller in a directive

  26. 26

    Calling directive function from controller angularjs

  27. 27

    How to call function in controller in angularjs?

  28. 28

    Call Controller Function from Directive (with params passed from directive)

  29. 29

    Call Controller Function from Directive (with params passed from directive)

HotTag

Archive