Ng-click在Directive Not Firing中定义

时间:2014-08-21 21:46:44

标签: javascript angularjs angularjs-directive

我正在尝试从我的指令模板中调用在我的指令类中定义的函数。

考虑以下模板:

<div class="ef-jcrop">
        <img
             src="placeholder"
             ng-attr-alt="{{image.ImageName}}"
             ng-src="{{image.url}}" />

        <table>
            <tr class="ratio-selection">
                <td class="ratio-item" ng-repeat="group in aspectRatioGroups">
                    <img ng-src="image.url" class='ratioSelector' ng-click="selectCrop(group)" />
                </td>
            </tr>
        </table>
</div>

问题是调用ng-click的{​​{1}},在我的指令的类中定义。

selectCrop()

对于那些不熟悉的人来说,这是TypeScript。然而,大多数它与JavaScript非常相似,因为我从JS翻译它。出于某种原因,module MyProject.Directives { export interface IEfJcropScope extends ng.IScope { image: MyProject.Image; aspectRatioGroups: _.Dictionary<ImageProductDimension[]>; } export class EfJcrop implements ng.IDirective { restrict = "E"; templateUrl = TEMPLATES + 'ef-jcrop.html'; replace = true; transclude = true; scope = { image: "=" } link = (scope: IEfJcropScope, elem, attrs) => {} controller = ($scope: IEfJcropScope) => {} selectCrop = (group: ImageProductDimension[]) => { . //my function is defined here } } } angular.module("mymodule").directive("efJcrop", () => new MyProject.Directives.EfJcrop()); 永远不会发生。这有什么理由让我失踪吗?

1 个答案:

答案 0 :(得分:1)

您需要将selectCrop函数附加到$ scope。我不知道如何编写TypeScript,但这里是JS:

$scope.selectCrop = function(){...}

$ scope是控制器和视图之间的粘合剂。此外,在为视图声明控制器时,需要注入$ scope