March 2015
Beginner to intermediate
212 pages
4h 31m
English
AngularJS controllers are at the center of AngularJS applications and are probably the most important component to understand. Controllers are not always clearly defined in some JavaScript client-side frameworks, and that tends to confuse developers who have experience with MVC frameworks. That is not the case with AngularJS. AngularJS clearly defines controllers, and controllers are at the center of AngularJS applications.
Almost everything that happens in an AngularJS application passes through a controller at some point. Dependency injection is used to add the needed dependencies, as shown in the following example file, which illustrates how to create a new controller:
/* chapter4/controllers.js - a new controller */varaddonsControllers=angular.module('addonsControllers',[]);addonsControllers.controller('AddonsCtrl',['$scope','checkCreds','$location','AddonsList','$http','getToken',functionAddonsCtrl($scope,checkCreds,$location,AddonsList,$http,getToken){if(checkCreds()!==true){$location.path('/loginForm');}$http.defaults.headers.common['Authorization']='Basic '+getToken();AddonsList.getList({},functionsuccess(response){console.log("Success:"+JSON.stringify(response));$scope.addonsList=response;},functionerror(errorResponse){console.log("Error:"+JSON.stringify ...
Read now
Unlock full access