javascript - How to load info of a function and save it in scope from resolve in a route using AngularJS -
how load info of function , save in scope resolve in route using angularjs?
for example:
.state('list.employee_list', { url: "/employee_list", templateurl: "views/list/employee_list.html", data: { pagetitle: 'employee list' }, resolve: { "employeescollection" : function(){ alert("load"); $scope.mydata = myfunction($scope); alert("finished load, load view"); } } })
the idea load info of table before loading view, table won't empty. function gets data parse.com
edit:
config.js
.state('list.employee_list', { url: "/employee_list", templateurl: "views/list/employee_list.html", data: { pagetitle: 'employee list' }, controller: employeelistctrl, resolve: { employeescollection : employeelistctrl.loaddata(companyid) } })
controllers.js
function employeelistctrl($scope, employeescollection){ $scope.employeescollection = employeescollection; } employeelistctrl.loaddata = function(companyid){ var employeescollection = []; var user = parse.object.extend("user"); var promise = new parse.promise(); var query = new parse.query(user); query.equalto("companyid", companyid); query.find().then(function(results) { (var = 0; < results.length; i++) { var estado = results[i].get("sendtest"); if(estado == "no"){ results[i].status = "{{ 'free' | translate }}"; }else if(estado == "yes"){ results[i].status = "{{ 'test_sent' | translate }}"; } console.log(results[i].get("lastname")); employeescollection[i] = results[i]; } }).then(function(redirect) { // done! //window.location.href = "#/list/employee_list"; promise.resolve(); console.log("loaded"); return employeescollection; }); }
views/list/employee_list.html
<tr ng-repeat="row in employeescollection"> <td> {{row.get("firstname")}} </td> <td> {{row.get("lastname")}} </td> <span class="label label-primary">{{status}}</span> </td> </tr>
here's how can accomplish it:
first, create service data. let's have simple service called mydata:
app.factory('mydata', ['$http', function ($http) { var obj = {}; obj.get = function (myendpoint) { return $http.get('https://some/endpoint/to/get/data/'+myendpoint).then(function (results) { return results.data; }); }; return obj; }]);
now can pass service resolve function in $routeprovider:
.when('/events', { templateurl: 'views/events.html', controller: 'eventsctrl', resolve: { preloaded: function(mydata) { return mydata.get('allevents'); } } })
and, in controller, can pass resolve object gain access data:
app.controller('eventsctrl', ['$scope', 'preloaded', function ($scope, preloaded) { $scope.allevents = preloaded; }]);
Comments
Post a Comment