angular controller无法看到指令中声明的函数

时间:2015-02-23 00:44:37

标签: javascript angularjs

这是html:

<div class="col-lg-8" ng-controller="usersCtrl">
<scrollable height="180" ts-attach-spinner class="list-group">
</scrollable>
</div>

这是指令:

(function() {
    'use strict';

    angular
        .module('angle')
        .directive('tsAttachSpinner', tsAttachSpinner);

    tsAttachSpinner.$inject = ['$window'];

    function tsAttachSpinner($window) {
        var directive = {
            link: link,
            restrict: 'A'
        };
        return directive;

        function link(scope, element, attrs) {
            function spinnerOff() {
                element.className = element.className + " whirl standard";
            }

            function spinnerOn() {
                element.className = element.className.replace(" whirl standard", "");
            }

            scope.spin = function (promises) {
                return $q.all(promises).then(function (eventArgs) {
                    spinnerOff();
                });
            }
        }
    }

})();

这是控制器:

(function () {
    'use strict';

    angular
        .module('angle')
        .controller('usersCtrl', usersCtrl);

    usersCtrl.$inject = ['$scope', 'usersSvc'];

    function usersCtrl($scope, usersSvc) {
        $scope.title = 'Users';
        activate();

        function activate() {
            var promises = [getUsers()];
            $scope.spin(promises);
        }

        function getUsers() {
            return usersSvc.getUsers().then(function (data) {
                return $scope.users = data;
            });
        }
    }
})();

如您所见,我声明了spin函数并将其附加到ts-attach-spinner指令中的作用域。但是,当我在控制器中调用$scope.spin时,我得到&#34; undefined不是函数&#34;

所有建议都表示赞赏,提前谢谢。

0 个答案:

没有答案