繁体   English   中英

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

[英]angularjs directive will not call controller function

我以前见过这些问题,通常是因为参数等,但我有一个非常简单的例子,我无法开始工作....

我有一个指令:

.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();
            });
        }
    };
});

模板看起来像这样:

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

    </div>
</div>

到目前为止非常简单,它只是一个指令,可以为文件输入创建一个很好的预览。 所以这个指令的声明在我看来是这样的:

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

在我的控制器中,我有这个功能:

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

    // Get our garment
    self.garment = garment;

    self.upload = function () {

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

所以,如果我打破这个:

  • 我的指令隔离了范围,并期望一个名为onComplete的回调函数,该函数与指令声明一起传递(即image-file-input =“callback()”)
  • 我的指令在范围。$ apply之后调用此函数,并且在检查回调函数存在之后,不传递任何参数
  • 我的视图传递了控制器函数upload(),它再次没有参数
  • 我的控制器有一个附加到名为upload的作用域的函数,其中包含console.log
  • 但没有任何事情被执行

有谁知道为什么?

您很可能忘记在ng-controller指定“控制器为”别名:

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

此外,没有必要进行此检查:

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

实际上, scope.onComplete永远不会被undefined ,因为Angular会创建一个函数调用包装器,即使未分配该属性也是如此。 你可以安全地拨打电话:

scope.onComplete();

暂无
暂无

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

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