4

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?

0

1 Answer 1

0

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();
Sign up to request clarification or add additional context in comments.

1 Comment

as it turns out, the problem I had was due to nested controllers. I had named my child controller graphicsController so the upload function was never being called.

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.