Implementing the AngularJS Module and Controller

With the views finished, you need to implement the AngularJS controller code to support them. Listing 26.15 implements the AngularJS controller that supports the user view. The controller, like the web forms, is very basic. The controller uses the $http service to get the user data from the /user/profile route and provide it to the AngularJS template in the $scope.user value.

Listing 26.15 my_app.js: Implementing an AngularJS controller to get user data by using an $http request


01 angular.module('myApp', []).
02   controller('myController', ['$scope', '$http',
03                               function($scope, $http) {
04     $http.get('/user/profile')
05         .success(function(data, status, headers, config) {
06       $scope.user = data;
07       $scope.error = "";
08     }).
09     error(function(data, status, headers, config) {
10       $scope.user = {};
11       $scope.error = data;
12     });
13   }]);


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.134.113