angular.js 中的 $window 在使用 $window.open 时抛出异常

Mon*_*lis 0 javascript angularjs

我抛出这个错误:

Error: Error: [$parse:isecwindow] Referencing the Window in Angular expressions is disallowed!
Run Code Online (Sandbox Code Playgroud)

当我尝试在 angularjs 中使用 $window.open/window.open 时。

生成.html

<div class="print-report-footer" ng-show="vm.clicked">
    <button type="button" class="btn btn-primary" ng-click="vm.downloadFile('pdf')">PDF</button>
    <button type="button" class="btn btn-primary" ng-click="vm.downloadFile('xls')">XLS</button>
    <button type="button" class="btn btn-primary" ng-click="vm.downloadFile('csv')">CSV</button>
</div>
Run Code Online (Sandbox Code Playgroud)

生成.ctrl.js

    function downloadFile ( fileType ) {
        var path = '/images/reports/DistrictSchoolReport.' + fileType;
        return $window.open( path );
    }

    self.downloadFile   = downloadFile;
Run Code Online (Sandbox Code Playgroud)

这是我使用过的代码。我需要怎么做才能避免每次使用 $window.open 时抛出这个错误?

bam*_*ide 5

您正在从视图中运行 $window.open()。改为这样做:(不要使用返回)

function downloadFile ( fileType ) {
    var path = '/images/reports/DistrictSchoolReport.' + fileType;
    $window.open( path );
}
Run Code Online (Sandbox Code Playgroud)