Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't get it working... #27

Open
eugenevk opened this issue Jul 4, 2016 · 2 comments
Open

Don't get it working... #27

eugenevk opened this issue Jul 4, 2016 · 2 comments

Comments

@eugenevk
Copy link

eugenevk commented Jul 4, 2016

Hi,
Can you please help? I am trying to implement the filesaver, but can't get it working. Nothing happens when I click the Download button.
I have the following defined in my index.html :

<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="js/angular-sanitize.min.js"></script>
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/angular-file-saver/dist/angular-file-saver.js"></script>
<script type="text/javascript" src="js/angular-file-saver/dist/angular-file-saver.bundle.js"></script>
<script type="text/javascript" src="app/app.js"></script>

The textarea in index.html looks as follows:

<div class="wrapper" ng-controller="FileSaveCtrl as vm">
  <textarea
    ng-model="vm.val.text"
    name="textarea" rows="5" cols="20">
      Hey ho let's go!
  </textarea>
  <a href="" class="btn btn-dark btn-small" ng-click="vm.download(vm.val.text)">
    Download
  </a>
</div>

The app.js is as follows:

var app = angular.module('psApp', [
        "ngSanitize",
        "ngFileSaver"
]);

app.controller('FileSaveCtrl', ['$scope', 'FileSaver', 'Blob', function FileSaveCtrl($scope, FileSaver, Blob) {
    $scope.val = {
        text: 'Hey ho lets go!'
    };

    $scope.download = function(text) {
        var data = new Blob([text], { type: 'text/plain;charset=utf-8' });
        FileSaver.saveAs(data, 'text.txt');
    };
}]);
@merrillmatt
Copy link

@eugenevk, it looks like the issue is related to mixing "attach to $scope" style with controller as syntax. Either remove the controller as syntax from your view or modify your controller so that it properly implements the controller as syntax.

@alferov
Copy link
Owner

alferov commented Jul 5, 2016

@eugenevk, @merrillmatt is right. In your case, the view template should look like

<div class="wrapper" ng-controller="FileSaveCtrl">
  <textarea
    ng-model="val.text"
    name="textarea" rows="5" cols="20">
      Hey ho let's go!
  </textarea>
  <a href="" class="btn btn-dark btn-small" ng-click="download(val.text)">
    Download
  </a>
</div>

P.S: There is no need to include angular-file-saver.js if angular-file-saver.bundle.js is included already.
angular-file-saver.bundle.js = angular-file-saver.js + polyfills (Blob.js, FileSaver.js)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants