Angularjs: push data into array from resource query and show in ngRepeat -
there array data retrieved restful api
using $resource
, fetch using query()
method. create new data , want push data created fetched array $resource
shown in ngrepeat
directive, when make push function, doesn't push , wouldn't re-query whole data again.
controller
self.submit = function(){ //this function called when submit data creation var form = self.form; form.user_id = global.user.id; // hide error message if it's shown self.data.error.show = false; // check if submition on request (for reason submit twice) if(!self.status.request){ self.status.request = true; $timeout(function(){ projects.save({data: form}, function(data){ //function called $resource console.log(data); self.status.request = false; // resolve error if(data.error[1]) resolveerror(); // merge data list else{ form.id = data.id; mergedata(form); self.closedialog(); } }); }, 1000) } // show error message function resolveerror(){ var error = self.data.error; error.show = true; } // merge form data list data function mergedata(data){ self.query.push(data); //this doesn't seem work console.log(data, self.query); //log see if data in array } }
view
<md-card ng-repeat="project in projects.query | orderby:'id':true"> <md-toolbar> <div class="md-toolbar-tools"> <h2>{{project.name}}</h2> <span flex></span> <h3>{{project.user_id}}</h3> </div> </md-toolbar> <md-card-content> <p>{{project.description}}</p> </md-card-content> <div class="md-actions" layout="row" layout-align="end center"> <md-button ng-href="#/projects/{{project.id}}">view</md-button> </div> </md-card>
note: data saved on database, , way see on list refreshing page
update: projects
alias of projectscontroller
Comments
Post a Comment