angularjs - Angular 1 dynamic form object -


i'm using angular 1 , creating dynamic form. works looping through objects , rendering dynamically binded input fields like:

<div class="quest-form form-group" ng-repeat="task in tasks">        <div ng-if="task.class == 'tasktext'" ng-class="'task ' + task.class">                   <input ng-model="questform.task[task.id].value" ng-name="task_{{task.id}}" ng-required="task.required == 1" type="text" class="form-control" placeholder="{{task.title}}"  />     </div>     ...     ... </div> 

i have upload field in loop:

<div ng-if="task.class == 'taskupload'" ng-class="'task ' + task.class">     <input class="btn btn-primary upload-btn" ngf-max-size="10mb" type="file" ng-model="upload" ngf-multiple="false" ngf-select="uploadfile(upload, task.id, $invalidfiles)" />     <input class="" ng-model="questform.task[task.id].fileuploadid" ng-required="task.required == 1" ng-name="task_{{task.id}}" type="text" /> </div> 

when file uploaded event called i'm trying set value of hidden field ng-model="questform.task[task.id].fileuploadid" this:

$scope.uploadfile = function(file,taskid) {     file.upload = upload.upload({         url: assetsurl+'/home/uploadfile',         data: {file: file}     });     file.upload.then(function (response) {         $scope.questform.task[taskid].fileuploadid = response.data; // messes     }, function (response) {         ...     }); }; 

i following error, it's $scope.questform.task[128] not exist though hidden input looks correct , binded $scope.questform.task[128].

angular.js:14362 typeerror: cannot read property '128' of undefined @ file.upload.then.$scope.errormsg (http://localhost/carl-mygps-app/js/controllers/quest-details-controller.js:120:26) @ processqueue (http://localhost/carl-mygps-app/bower_components/angular/angular.js:16689:37) @ http://localhost/carl-mygps-app/bower_components/angular/angular.js:16733:27 @ scope.$eval (http://localhost/carl-mygps-app/bower_components/angular/angular.js:18017:28) @ scope.$digest (http://localhost/carl-mygps-app/bower_components/angular/angular.js:17827:31) @ scope.$apply (http://localhost/carl-mygps-app/bower_components/angular/angular.js:18125:24) @ done (http://localhost/carl-mygps-app/bower_components/angular/angular.js:12233:47) @ completerequest (http://localhost/carl-mygps-app/bower_components/angular/angular.js:12459:7) @ xmlhttprequest.requestloaded (http://localhost/carl-mygps-app/bower_components/angular/angular.js:12387:9) possibly unhandled rejection: {} 

i have tried defining blank objects in scope like:

$scope.questform = []; $scope.questform.task = {}; 

but should not need because created in template? confused. all.

actually nope. having template compiled not mean ng-models initialized. while ng-model smart enough create intermediate objects, if don't exist, doesn't until $viewvalue changed. in case if upload file without editing other input first, $viewvalue inputs has never changed, , have initialize questform, questform.task, , questform.task[taksid] yourself.

if (!$scope.questform) {   $scope.questform = {}; } if (!$scope.questform.task) {   $scope.questform.task = {}; } if (!$scope.questform.task[taskid]) {   $scope.questform.task[taskid] = {}; } $scope.questform.task[taskid].fileuploadid = response.data; 

or can initialize questform , questform.task @ beginning. , check if questform.task[taskid] exists before initializing it.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -