簡體   English   中英

Angular控制器功能無法通過指令運行

[英]Angular controller functions won't work from directive

大家好,我有一個小問題,我正在嘗試在指令內聲明的控制器中調用某些函數,如下所示:

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

但是,當我調用模板中的函數時,似乎什么也沒發生。.我確定自己缺少了一些東西,但是呢?

答案是范圍:錯誤,不知道為什么:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM