How to Write AngularJS Frontends for LoopBack Applications
Niklas Heidloff provides an example of how to write AngularJS front-end against the Loopback Node.js API framework.
Join the DZone community and get the full member experience.
Join For Freewith the node.js api framework loopback you can easily provide rest apis for your applications. in order to build web frontends, loopback provides an angularjs javascript sdk . below is a sample application which demonstrates how to use this sdk.
download the sample application from github.
in angularjs applications, you typically build views that interact with services via controllers. the services access data from the server-side applications. obviously, you can write your own services which invoke the rest apis of your loopback applications directly. but, loopback makes this even simpler via the angularjs sdk. with the sdk, client-side javascript libraries are generated for your rest apis so that the controllers can simply invoke javascript apis rather than rest apis.
the sample application demonstrates how to build a web-based frontend for a simple approval request scenario. approval requests have titles, descriptions, requesters, and approvers.
here is the code snippet of the controller that returns all approval requests. the class approvalrequest is provided by the loopback sdk.
angular.module('angularapp').controller('requestscontroller', ['$scope', 'approvalrequest', function($scope, approvalrequest) {
$scope.requests = [];
function getrequests() {
approvalrequest.find().$promise
.then(function(results) {
$scope.requests = results;
},
function(err) {
$scope.requestserror = err.statustext;
}
);
}
getrequests();
}]);
authentication is also very easy from angular. just invoke the person.login function.
the file lb-services.js with the javascript apis for your own business objects defined via loopback is generated either via command line or via grunt . the approval request sample application uses the grunt plugin to build the file automatically and to put it in the angular application.
Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments