角度控制器功能不受指令的影响

时间:2016-05-10 09:36:47

标签: javascript angularjs

嘿伙计们我有一个小问题,我试图将一些函数称为在指令内声明的控制器,如下所示:

var homepage = angular.module('homepage');

homepage.directive('searchServices', [function () {
    return {
        scope: {},
        replace: true,
        controller: ['$scope', function ($scope) {
            $scope.services = config.services;
            $scope.currentService = $scope.services[0];

            $scope.clickMe = function() {
                console.log('xxxxx');
            }

            $scope.setCurrentService = function (service) {
                console.log(service);
                return service == $scope.currentService;
            }

            $scope.setNewService = function (e, service) {
                e.preventDefault();
                console.log(service);
                $scope.currentService = service;
            }
        }],
        template: [
            '<li ng-repeat="service in services"',
                 'ng-click="clickMe()"',
                 'ng-class="{active:setCurrentService(currentService)}">',
                 '<a href="#">',
                     '{{currentService}}',
                     '<span class="icons-{{service}}"></span>',
                      '{{service}}',
                 '</a>',
            '</li>'].join('')
    }    
}]);

但是当我调用模板中的函数时似乎没有发生任何事情。我确信我错过了什么......但是什么?

1 个答案:

答案 0 :(得分:0)

答案是范围:假,不知道为什么:)