Angular js指令使用控制器作为语法ng-click不起作用

时间:2015-03-03 19:27:41

标签: javascript angularjs directive

我无法在具有隔离范围的指令中使用"控制器作为" Angular 1.3中的语法指令的代码如下:

myDirectives.directive('gsphotosquare', dirfxn);

function dirfxn() {
    var directive = {
        replace: false,
        scope: {
            photoInfo: '=',
            photoBatchNum: '=',
            thumbnailwidth: '='
        },
        restrict: 'EA',
        controller: Ctrller,
        controllerAs: 'ctrl',
        template: '<div ng-click="ctrl.squareClicked()">test</div>',
        //templateUrl: 'views/directives/gsphotosquare.html',
        bindToController: true, // because the scope is isolated
        link: linkFunc //adding this didn't help
    };
    return directive;
}

function Ctrller() {
    var vm = this;

    vm.squareClicked = function () {
        alert('inside clickhandler for gsphotosquare directive');
    };
}

function linkFunc(scope, el, attr, ctrl) {

    el.bind('click', function () {
        alert('inside clickhandler for gsphotosquare directive');
    });
}

以下是DOM中如何使用该指令:

 <span class="input-label">General Site Photos</span>
    <div class=" item row">
    <gsphotosquare photo-info="mt.photos.v1f1[0]" photo-batch-num="mt.photoBatchNum" ></gsphotosquare>
    <gsphotosquare photo-info="mt.photos.v1f1[1]" photo-batch-num="mt.photoBatchNum" ></gsphotosquare>
    <gsphotosquare photo-info="mt.photos.v1f1[2]" photo-batch-num="mt.photoBatchNum" ></gsphotosquare>
   <gsphotosquare photo-info="mt.photos.v1f1[3]" photo-batch-num="mt.photoBatchNum" ></gsphotosquare>
</div>

为什么点击渲染指令的任何想法都没有显示警告?

1 个答案:

答案 0 :(得分:0)

尝试以不同的方式定义控制器:

myDirectives.controller('Ctrller', Ctrller);

然后在你的指令中:

controller: 'Ctrller as ctrl',