AngularDart ngFor with input change

Issue

Tasks structure like: <Map<String,String>>[{'_tasknumber':'123'}]

Repository

@Component(
    template: '''
    <div *ngFor="let task of tasks">
                <label for="fileInput">
                    <material-button>
                        Add image 
                    </material-button>
                </label>

          id: {{task['_tasknumber']}}

          <input type="file"
                id="fileInput"
                multiple
                #fileInput
                (change)="uploadFilesForTask(fileInput.files, task['_tasknumber'])"/>
    </div>
    ''')

In function uploadFilesForTask I just print taskId value:

Future<Null> uploadFilesForTask(
      List<File> files, String taskId) async {
    print(taskId);
}

When I press “Add image” button I every time get id of first task in list of tasks.

When I press input button “Choose files” I get right id what I need.

How I can get right id of task by “Add image” button?

pubspec.yaml:

environment:
  sdk: '>=1.24.2'

dependencies:
  angular: '^4.0.0'
  angular_forms: '^1.0.0'
  angular_router: '^1.0.2'
  angular_components: '^0.8.0'

Solution

I use not unique label for attribute.

Not right:

<label for="fileInput">
                    <material-button>
                        Add image 
                    </material-button>
                </label>

          <input type="file"
                id="fileInput"
                multiple
                #fileInput
                (change)="uploadFilesForTask(fileInput.files, task['_tasknumber'])"/>

Right:

<label [attr.for]="task['_tasknumber']">
                    <material-button>
                        Add image 
                    </material-button>
                </label>

          <input type="file"
                [attr.id]="task['_tasknumber']"
                multiple
                #fileInput
                (change)="uploadFilesForTask(fileInput.files, task['_tasknumber'])"/>

Answered By – Vitaliy Vostrikov

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.