【问题标题】:How can I read and write an Angular component variable from Javascript?如何从 Javascript 读取和写入 Angular 组件变量?
【发布时间】:2018-01-11 04:50:01
【问题描述】:

我已经创建了一个带有表单和文件上传器的组件。

我需要修改一个打字稿变量以禁用"Done" 按钮。我的意思是,我需要防止我的表单在上传完成之前提交。

代码:

(跟随上传变量)

import { Component, OnInit} from '@angular/core';
declare var $: any;

@Component({
    selector: 'app-child-add-moment',
    templateUrl: './child-add-moment.component.html',
    styleUrls: ['./child-add-moment.component.css']

})
export class ChildAddMomentComponent implements OnInit {

    private FILE_UPLOAD_URL = 'http://localhost/uploader/ajax_upload_file.php';

    uploading = false;
    uploadedFiles = 0; // I need change this variable from $('#filer_input2') success function

    constructor() { }

    ngOnInit() {
        this.initializeUploader();
    }

    initializeUploader(): void {

        $('#filer_input2').filer({
            changeInput: '<a class="btn btn-primary">Add photo</a>',
            showThumbs: true,
            theme: 'dragdropbox',
            templates: {
            box: '<ul class="jFiler-items-list jFiler-items-grid"></ul>',
            item: '<li class="jFiler-item">{{fi-name}}</li>',
            itemAppend: '<li class="jFiler-item">{{fi-name}}</li>',
            progressBar: '<div class="bar"></div>',
            itemAppendToEnd: false,
            canvasImage: true,
            removeConfirmation: true,
            _selectors: {
                list: '.jFiler-items-list',
                item: '.jFiler-item',
                progressBar: '.bar',
                remove: '.jFiler-item-trash-action'
            }
        },
        uploadFile: {
            url: this.FILE_UPLOAD_URL,
            data: null,
            type: 'POST',
            enctype: 'multipart/form-data',
            synchron: true,
            beforeSend: function() {

                // This show FALSE value but this value is not from "uploading" var from the component. 
                console.log(this.uploading);

                if (!this.uploading) {
                    this.uploadedFiles = 0;
                }

                // I set this variable to TRUE to DISABLE some buttos from my template. But this setter, dont change the value of the "uploading" variable from the component.
                this.uploading = true;

            },
            success: function(data, itemEl, listEl, boxEl, newInputEl, inputEl, id)
            {
                this.uploadedFiles++;

                const filerKit = inputEl.prop('jFiler');

                filerKit.files_list[id].name = new_file_name;

                if (filerKit.files_list.length === this.uploadedFiles) {
                    console.log('All files have been uploaded');

                    // When upload has been finished, I set this variable to FALSE to ENABLE some buttons again.
                    this.uploading = false;

                    this.uploadedFiles = 0;
                }

            },
            error: null,
            onComplete: null
        },   
        dialogs: null,
        captions: {
            button: 'Choose Files',
            feedback: '"Choose files To Upload',
            feedback2: 'files were chosen',
            drop: 'Drop file here to Upload',
            removeConfirmation: '"Are you sure you want to remove this file?',
            errors: null
        }
    });

}

如何从“beforeSend”和“success”函数修改“uploading”变量?

重要的一点:我想将我的 var“上传”与模板绑定。

用于启用和禁用按钮:

&lt;button type="button" class="btn btn-primary" [class.disabled]="uploading"&gt; Add &lt;/button&gt;

谢谢

【问题讨论】:

  • 一旦成功,将this.loading 更改为完成,并在您的html Done 按钮条件中输入[hidden]="loading" 之类的条件
  • 这个 Jquery 库是否创建了您要禁用的按钮? changeInput: '添加照片'

标签: javascript angular typescript


【解决方案1】:

您需要在类方法中保存对该类的引用,然后从回调中调用它:

在类方法级别:

export class ChildAddMomentComponent implements OnInit {
    uploading = false;

    initializeUploader(): void {
        var self = this;
        // some code
    }
}

在你的回调中:

  self.uploading = true; 

【讨论】:

  • 感谢您的回答,但是当我在回调中声明“self.uploading = true;”时,打字稿编译器会说:“'Window' 类型上不存在属性'上传'。”
  • 好的,在这种情况下,在你的 initializeUploader 方法中试试这个:var self = this;
  • 有效@Marventus !! 我添加了“const self = this;”在 initializeUploader 中,编译器错误消失了。谢谢!
  • 太棒了!我已经相应地修改了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
相关资源
最近更新 更多