javascript - Ionic, List populating via search -
i using backemnd service (parse in case doesn't matter question) , wanted search it. have textbox upon text being entered searches server , returns array of matchs.
my next step display returned objects nicely in list. easy enough ng-repeat because view has been loaded ui won't update reflect array being loading list. make sense?
i wondering if there technique refresh list , show returned search elements, , not being greedy here doing in way looks , not clunky.
i did lot of googling no luck :( advice amazing.
without code provided hard guess wrong. angular has two-way binding, view should updated automatically after changing content of array. if it's not, means did wrong in code. present example code should work in case.
controller
angular.module('modulename') .controller('viewcontroller', ['viewservice', viewcontroller]); function viewcontroller(viewservice) { var self = this; self.arraywithdata = []; self.searchtext = ""; // ---- public functions ---- self.searchdata = searchdata; // function loads data service function searchdata(searchtext) { viewservice.getdata(searchtext).then(function(dataresponse) { // clear array data self.arraywithdata.splice(0); // fill again new data response angular.foreach(dataresponse, function(item) { self.arraywithdata.push(item); }); }); } // --- private functions --- // controller initialization function _initialize() { self.searchdata(self.searchtext); } _initialize(); }
view
<div ng-controller="viewcontroller view"> <input type="text" ng-model="view.searchtext" /> <input type="button" value="search!" ng-click="view.searchdata(view.searchtext)" /> <!-- simple ngrepeat --> <div ng-repeat="item in view.arraywithdata"> <!-- want item --> </div> </div>
conclusion
by using splice() , push() make sure reference array not changed. if using controlleras syntax (as in example), assigning new data '=' work. however, if using $scope store data in controller, losing reference array probable reason why code doesn't work.
Comments
Post a Comment