【问题标题】:Error Called a function from a directive inside a function in a Controller错误从控制器中函数内部的指令调用函数
【发布时间】:2016-07-22 10:55:00
【问题描述】:

我有两个控制器,我做几乎相同的事情,只是改变了一点点,我有这个:

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)在其他控制器中执行了很多次,并且执行没有问题。

【问题讨论】:

  • 似乎 CreateSignerCtrl 控制器中的 $scope 不是从您的指令继承的。将 getModelData 函数移到服务中不是更好吗?
  • 也许可以,但该函数是一个标准函数,可用于任何模型解析来自模型或服务的第一个控制器的 url。

标签: javascript angularjs angularjs-directive angularjs-scope


【解决方案1】:

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

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

但是,我确实认为您应该将 getModelData 函数移到服务中。

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

【讨论】:

  • 不是那个,该指令都不包含在Controller中
  • 我尝试更新我的观点或那一刻的观点
  • 如果你能用你的 html 更新你的问题,我想看看你的标记,我可以更好地帮助解决你的问题。
  • 是的,老兄,html 和脚本 ;)
猜你喜欢
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 2015-12-08
  • 1970-01-01
  • 2016-03-14
相关资源
最近更新 更多