简体   繁体   English

angularjs指令不会调用控制器函数

[英]angularjs directive will not call controller function

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 (ie image-file-input="callback()") 我的指令隔离了范围,并期望一个名为onComplete的回调函数,该函数与指令声明一起传递(即image-file-input =“callback()”)
  • My directive calls this function after a scope.$apply and after checking the callback function exists, no parameters are passed 我的指令在范围。$ apply之后调用此函数,并且在检查回调函数存在之后,不传递任何参数
  • My view passes the controller function upload() which again has no parameters 我的视图传递了控制器函数upload(),它再次没有参数
  • My controller has a function attached to the scope called upload, which has a console.log in it 我的控制器有一个附加到名为upload的作用域的函数,其中包含console.log
  • Yet nothing gets executed... 但没有任何事情被执行

Does anyone know why? 有谁知道为什么?

You have most likely forgotten to specify a "controller as" alias in ng-controller : 您很可能忘记在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. 实际上, scope.onComplete永远不会被undefined ,因为Angular会创建一个函数调用包装器,即使未分配该属性也是如此。 You can safely make the call: 你可以安全地拨打电话:

scope.onComplete();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM