【问题标题】:Angularjs: Updating contents read from file into textareaAngularjs:将从文件读取的内容更新到textarea
【发布时间】:2014-12-04 19:28:49
【问题描述】:

我正在尝试创建一个文本区域,可以在其中写入其内容,但也可以通过 url 或上传的纯文本文件填充。所有这一切都只需要 AngularJS。
我能够将文件内容加载到链接到 textarea 的同一个变量中,但是,在获取带有“get”的 URL 内容到变量中时,会自动将 textarea 的内容更改为给定的文本,上传文件时它不会更新.
所以,对于初学者来说,我的 HTML:

<div id="prot2dna" ng-app="serverExe" ng-controller="p2dCTRL as p2d">
    <form novalidate>
        <!-- TEXTAREA -->
        <textarea class="form-control" ng-model="p2d.query.fasta"></textarea>
        <!-- URL SEARCH -->
        <div class="input-group">
        <input type="text" class="form-control" ng-model="p2d.query.uniac">
            <span class="input-group-btn">
                <button class="btn btn-default" type="button" ng-click="p2d.searchUNIP(p2d.query)">
                    Search
                </button>
            </span>
        </div>
        <!-- READ FILE -->
        <span class="btn btn-default my-btn btn-block btn-file">
            UPLOAD FILE
            <input type="file" on-read-file="p2d.query.fasta">
            <!-- Notice here is the same variable that the one linked to textarea -->
        </span>
    </form>
<div class="checkoutside">
<!-- Here I can see that the variable content is updated, regardless of the fact that it is displayed or not inside the textarea -->
content:<br>{{p2d.query.fasta}}
</div>
</div>

还有 javascript 代码:

var exeApp = angular.module('serverExe', [])
/* THIS DIRECTIVE IS USED TO LOAD THE CONTENT FROM A FILE
   IT ACTUALLY UPDATES THE VARIABLE this.query.fasta AND I CAN SEE THAT IN THE .checkoutside
   DIVISION, BUT NOTHING HAPPENS INSIDE THE textarea */
.directive('onReadFile', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            console.log(attrs)
            var model = $parse(attrs.onReadFile); 
            console.log(model)
            var modelSetter = model.assign;      

            element.bind('change', function(e) {
                scope.$apply(function(){
                    var reader = new FileReader();
                    reader.onload = function(){

                        modelSetter(scope, reader.result);
                    }
                    reader.readAsText(element[0].files[0])
                });
            });
        }
    };
})
.controller('p2dCTRL', function($http, $scope){
    // QUERY
    this.query = { 'fasta' : '', 'uniac' : '', 'fastafile': false, 'unierr': true };

    //GET CONTENT THROUGH URL
    this.searchUNIP = function(query) {
        url =  'http://www.uniprot.org/uniprot/'+this.query.uniac+'.fasta';
        $http.get(url)
            .success(function(data){
                // Assign content to variable -> this actually updates the content of the textarea
                query.fasta = data; 
            }).error(function(data){
                query.unierr = true;
            });
    };
});

现在,我完全不知道如何做到这一点......

非常感谢。

【问题讨论】:

  • 我不确定,但您是否尝试过将 this.query 更改为 $scope.query?
  • 是的,我虽然是这样,因为大多数“读取文件”示例都是这样工作的,所以尝试了一下......没有任何改变。但也许我做错了什么......

标签: javascript angularjs textarea


【解决方案1】:

啊。一种方法是像这样使用 $parse:

var onFileReadFn = $parse(attrs.onReadFile);
var reader = new FileReader();

reader.onload = function() {

    var fileContents = reader.result;

    // invoke parsed function on scope
    scope.$apply(function() {
        onFileReadFn(scope, {
            'contents' : fileContents
        });
    });
};

reader.readAsText(element[0].files[0]);

此处为完整示例:http://jsfiddle.net/200eoamf/1

【讨论】:

  • 是的!这样可行!!谢谢你的jsfiddle!那里看起来很清楚!
猜你喜欢
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多