错误从Controller中的函数内的指令调用函数

时间:2016-04-02 17:30:06

标签: javascript angularjs angularjs-directive angularjs-scope

我有两个控制器,我几乎做同样的事情,只改变其中的一些东西,我有这个:

app.controller('CreateSignerCtrl', ['$scope','SweetAlert','SignerResource','$http', '$location', '$routeParams','$timeout','ngDialog', function ($scope, SweetAlert, SignerResource, $http, $location, $routeParams, $timeout,ngDialog) {
            $scope.title = "Add Signer";
            $scope.button = "Save";
            $scope.Signer = {};
            $scope.cancel = ngDialog.closeAll;

            $scope.saveSigner = function() {
                SignerResource.save($scope.Signer);
                $.notify({
                    title: '<strong>Data Updated! </strong>',
                    message: 'Signer Updated.'
                },{
                    type: 'success',
                    placement: {
                        from: 'bottom',
                        align: 'right'
                    },
                    animate: {
                        enter: 'animated fadeInDown',
                        exit: 'animated fadeOutUp'
                    }
                });
                ngDialog.closeAll();
                $scope.getModelData();
            };
            $scope.save = $scope.saveSigner;
        }]);

在此控制器中,当我调用函数 $ scope.saveSigner 时,我收到此错误:

TypeError: $scope.getModelData is not a function

在此控制器中:

app.controller('EditSignerCtrl', ['$scope','SweetAlert','SignerResource','$http', '$location', '$routeParams','$timeout','ngDialog', function ($scope, SweetAlert, SignerResource, $http, $location, $routeParams, $timeout,ngDialog) {
        $scope.title = "Edit Signer";
        $scope.button = "Update";
        $scope.Signer = SignerResource.get({
            id: $scope.signerId
        });
        $scope.cancel = ngDialog.closeAll;

        $scope.updateSigner = function() {
            SignerResource.update($scope.Signer);
            $.notify({
                title: '<strong>Data Updated! </strong>',
                message: 'Signer Updated.'
            },{
                type: 'success',
                placement: {
                    from: 'bottom',
                    align: 'right'
                },
                animate: {
                    enter: 'animated fadeInDown',
                    exit: 'animated fadeOutUp'
                }
            });
            ngDialog.closeAll();
            $scope.getModelData();
        };

        $scope.save = $scope.updateSigner;
    }]);

在此控制器中,所有操作都正常, $ scope.getModelData 不会向我发送任何错误。

指令是这样的:

app.directive('datapaginator', function() {
  return {
    url: '=a',
    controller: function($scope, $http) {

        $scope.model = [];

        $scope.getModelData = function() {
            $http({
                url: $scope.url,
                method: "GET",
                params: {page:  $scope.currentpage || 1}
            }).success(function(data, status, headers, config) {
                if($scope.currentpage > data.last_page){
                    $scope.currentpage = data.last_page;
                    $scope.getModelData();
                }
                $scope.currentpage = data.current_page;
                $scope.lastpage = data.last_page;
                $scope.generatePages(data.last_page);
                $scope.model = data.data;
            }).error(function(response) {
                $scope.model = response.error;
            });
        };
});

该函数( $ scope.getModelData )在其他控制器中多次执行,执行时没有问题。

1 个答案:

答案 0 :(得分:0)

我认为您的指令 datapaginator 不包含在您的** CreateSignerCtrl **中,但它包含在 EditSignerCtrl 中。

  

您应该检查您的标记,看看您是否在两个控制器中都包含了您的指令。

     

但是,我认为您应该将 getModelData 功能转移到服务中。

阅读this。希望我能帮忙:)。