调用控制器函数 - angularjs

时间:2018-03-21 17:59:45

标签: html angularjs twitter-bootstrap

想要从指令标签调用控制器功能。

演示:https://jsfiddle.net/6qqfv61k/

点击'导出到Excel'我想从appCtrl调用dataToExport(),因为数据可以导出。任何输入?

HTML:

<div ng-controller="appCtrl">
 </div>
 <div excel-export export-data="exportData" file-name="{{fileName}}"></div>

js code:

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

app.controller('appCtrl', ['$scope', function($scope) {
$scope.dataToExport = function(){
    $scope.jsonToExport = [
    {
        "col1data": "1",
      "col2data": "Fight Club",
      "col3data": "Brad Pitt"
    },
    {
        "col1data": "2",
      "col2data": "Matrix (Series)",
      "col3data": "Keanu Reeves"
    },
    {
        "col1data": "3",
      "col2data": "V for Vendetta",
      "col3data": "Hugo Weaving"
    }
  ];

    // Prepare Excel data:
    $scope.fileName = "report";
    $scope.exportData = [];
  // Headers:
    $scope.exportData.push(["#", "Movie", "Actor"]);
  // Data:
    angular.forEach($scope.jsonToExport, function(value, key) {
    $scope.exportData.push([value.col1data, value.col2data, value.col3data]);
    });
  }
}]);
//directive

2 个答案:

答案 0 :(得分:0)

使用&amp;在指令的范围选项中。 &安培;绑定非常适合将回调函数绑定到指令。将函数作为回调函数传递给指令

在控制器范围内移动指令

stepNumberVule

指令

<div ng-controller="appCtrl">
  <div excel-export export-data="exportData" file-name="{{fileName}}" call-back="dataToExport()"></div>
</div>

在指令的click方法中调用callBack()

  .directive('excelExport',
    function () {
      return {
        restrict: 'A',
        scope: {
            fileName: "@",
            data: "&exportData",
            callBack: "&"
        },..

答案 1 :(得分:0)

您可以将dataToExport定义为指令范围函数。请看看

<div ng-controller="appCtrl">
 <div excel-export export-data="exportData" export="dataToExport()" file-name="{{fileName}}"></div>
</div>

app
  .directive('excelExport',
    function () {
      return {
        restrict: 'A',
        scope: {
            fileName: "@",
            data: "&exportData",
            dataToExport: '&export'
        },
        replace: true,
        template: '<button class="btn btn-primary btn-ef btn-ef-3 btn-ef-3c mb-10" ng-click="download()">Export to Excel <i class="fa fa-download"></i></button>',
        link: function (scope, element) {

            scope.download = function() {
                scope.dataToExport();
                function datenum(v, date1904) {
                    if(date1904) v+=1462;
                    var epoch = Date.parse(v);
                    return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
                };

                function getSheet(data, opts) {
                    var ws = {};
                    var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
                    for(var R = 0; R != data.length; ++R) {
                        for(var C = 0; C != data[R].length; ++C) {
                            if(range.s.r > R) range.s.r = R;
                            if(range.s.c > C) range.s.c = C;
                            if(range.e.r < R) range.e.r = R;
                            if(range.e.c < C) range.e.c = C;
                            var cell = {v: data[R][C] };
                            if(cell.v == null) continue;
                            var cell_ref = XLSX.utils.encode_cell({c:C,r:R});

                            if(typeof cell.v === 'number') cell.t = 'n';
                            else if(typeof cell.v === 'boolean') cell.t = 'b';
                            else if(cell.v instanceof Date) {
                                cell.t = 'n'; cell.z = XLSX.SSF._table[14];
                                cell.v = datenum(cell.v);
                            }
                            else cell.t = 's';

                            ws[cell_ref] = cell;
                        }
                    }
                    if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
                    return ws;
                };

                function Workbook() {
                    if(!(this instanceof Workbook)) return new Workbook();
                    this.SheetNames = [];
                    this.Sheets = {};
                }

                var wb = new Workbook(), ws = getSheet(scope.data());
                /* add worksheet to workbook */
                wb.SheetNames.push(scope.fileName);
                wb.Sheets[scope.fileName] = ws;
                var wbout = XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'binary'});

                function s2ab(s) {
                    var buf = new ArrayBuffer(s.length);
                    var view = new Uint8Array(buf);
                    for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
                    return buf;
                }

                saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), scope.fileName+'.xlsx');

            };

        }
      };
    }
 );