Angularjs指令 - 未定义exportTable

时间:2017-09-12 20:46:05

标签: javascript angularjs

我有关于将指令附加到我的模块的小问题。我们来看看:

我有controller.js:

var app = angular.module("app", []);

app.controller('someCtrl', function($scope) {

 $scope.exportAction = function(){
       switch($scope.export_action){
           case 'pdf': $scope.$broadcast('export-pdf', {});
                       break;
           case 'excel': $scope.$broadcast('export-excel', {});
                       break;
           case 'doc': $scope.$broadcast('export-doc', {});
                       break;
           default: console.log('no event caught');
        }
     }

 });

当我想在这个指令下面附上时:

 app.directive('exportTable', function() {
 var exportTable = function(){

 var link = function($scope, elm, attr){

 $scope.$on("export-pdf", function(e, d){
       elm.tableExport({type:'pdf', escape:'false'});
  });

 $scope.$on("export-excel", function(e, d){
        elm.tableExport({type:'excel', escape:false});
  });

 $scope.$on("export-doc", function(e, d){
      elm.tableExport({type: 'doc', escape:false});
  });

 }

 return {
   restrict: 'C',
   link: link
    }
  }
});

我有“错误:指令未定义”。我怎么附上这个?

1 个答案:

答案 0 :(得分:0)

刚刚复制了您的代码,我发现它没有任何问题。

var app = angular.module("app", []);
app.controller('someCtrl', function ($scope) {
    $scope.exportAction = function(){
        switch($scope.export_action){
            case 'pdf': $scope.$broadcast('export-pdf', {});
                break;
            case 'excel': $scope.$broadcast('export-excel', {});
                break;
            case 'doc': $scope.$broadcast('export-doc', {});
                break;
            default: console.log('no event caught');
        }
    }

}).directive('exportTable', function() {
    var exportTable = function(){

        var link = function($scope, elm, attr){

            $scope.$on("export-pdf", function(e, d){
                elm.tableExport({type:'pdf', escape:'false'});
            });

            $scope.$on("export-excel", function(e, d){
                elm.tableExport({type:'excel', escape:false});
            });

            $scope.$on("export-doc", function(e, d){
                elm.tableExport({type: 'doc', escape:false});
            });

        }

        return {
            restrict: 'C',
            link: link
        }
    }
});