Introduction to AngularJS AngularJS is a JavaScript framework. It can be added to HTML pages via the tag. <br> </p> <p>AngularJS extends HTML through directives and binds data to HTML through expressions. <br> </p> <p><span style="color: #0000ff"><strong>What is AngularJS? </strong></span></p> <p>AngularJS makes it easier to develop modern single page applications (SPAs: Single Page Applications). <br> </p> <p>AngularJS binds application data to HTML elements. <br> </p> <p>AngularJS can clone and repeat HTML elements. <br> </p> <p>AngularJS can hide and show HTML elements. <br> </p> <p>AngularJS can add code "behind" HTML elements. <br> </p> <p>AngularJS supports input validation. </p> <p>Usually, JSON is used as the model for storing data in AngularJS. We may write the model in the controller like this: </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;"> app.controller('BookController',['$scope',function($scope){ $scope.book = { id:1, name:'', author:'', stores:[ {id:1, name:'', quantity:2}, {id:2, name:'', quantity:2}, ... ] }; }])</pre><div class="contentsignin">Copy after login</div></div> </div> <p>This model may be used in the view like this:<br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;"> <div ng-controller="BookController"> <span ng-bind="book.id"></span> <input type="text" ng-model="book.name"/> <input type="text" ng-model="book.author"/> </div> </pre><div class="contentsignin">Copy after login</div></div> </div> <p>When we need to get data from the server, we may write like this: </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;"> app.controller('BookController',['$scope', '$http', function($scope, $http){ var bookId = 1; $http.get('api/books'+bookId).success(function(bookData){ $scope.book = bookData; }) $scope.deleteBook = function(){ $http.delete('api/books/' + bookId); } $scope.updateBook = function(){ $http.put('api/books/'+bookId, $scope.book); } $scope.getBookImageUrl = function(width, height){ return 'our/iamge/service' +bookId + '/width/height'; } $scope.isAvailable = function(){ if(!$scope.book.stores || $scope.book.stores.length === 0){ return false; } reutrn $scope.book.stores.some(function(store){ return store.quantity > 0; }) } }]) </pre><div class="contentsignin">Copy after login</div></div> </div> <p> may be used like this in a view: </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;"> <div ng-controller="BookController"> <div ng-style="{backgroundImage: 'url('+getBookImageUrl(100,100)+')'}"></div> <span ng-bind="book.id"></span? <input type="text" ng-model="book.name"/> <input type="text" ng-model="book.author"/> is available: <span ng-bind="isAvailable() ? 'Yes' : 'No'"></span> <button ng-click="deleteBook()">Delete</button> <button ng-click="updateBook">Update</button> </div> </pre><div class="contentsignin">Copy after login</div></div> </div> <p> Above, the JSON format model can only be used in BookController. How can it be used in other controllers? <br /> --By factory method</p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;"> app.factory('Book', ['$http', function($http){ function Book(bookData){ if(bookData){ this.setData(bookData); } } Book.prototype = { setData: function(bookData){ angular.extend(this, bookData); }, load: function(id){ var scope = this; $http.get('api/books/' + bookId).success(function(bookData){ scope.setData(bookData); }) }, delete: function(bookId){ $http.delete('api/books/' + bookId); }, update: function(bookId){ $http.put('api/books/' + bookId, this); }, getImageUrl: function(width, height){ return 'our/image/service/' + this.book.id + '/' + width + '/' + height; }, isAvailable: funciton(){ if(!this.book.stores || this.book.stores.length === 0) { return false; } return this.book.stores.some(function(store){ return store.quantity > 0; }) } } return Book; }]) </pre><div class="contentsignin">Copy after login</div></div> </div> <p>Above, a Data Model similar to Book has been created through factory, which can now be injected into the controller. <br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;"> app.controller('BookController', ['$scope', 'Book', function($scope, Book){ $scope.book = new Book(); $scope.book.load(1); }]) </pre><div class="contentsignin">Copy after login</div></div> </div> <p> There will also be corresponding changes in the view. </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;"> <div ng-controller="BookController"> <div ng-style="{backgroundImage: 'url(' + book.getImageUrl(100, 100) + ')'}"></div> <span ng-bind="book.id"></span> <input type="text" ng-model="book.name"/> <input type="text" ng-model="book.author"/> is abailble: <span ng-bind="book.isAvailabe() ? 'Yes' : 'No'"></span> <button ng-click="book.delete()">Delete</button> <button ng-click="book.update()">Update</button> </div> </pre><div class="contentsignin">Copy after login</div></div> </div> <p>As above, multiple controllers can use the same Data Model about books. What if multiple controllers process the same Data Model about books? </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;"> app.factory('booksManager', ['$http', '$q', 'Book', function($http. $q, Book){ var booksManager = { _pool: {}, _retrieveInstance: function(bookId, bookData){ var instance = this._pool[bookId]; if(instance){ instance.setData(bookData); } else { instance = new Book(bookData); this._pool[bookId] = instance; } return instance; }, _seach: function(bookId){ reutrn this_.pool[bookId]; }, _load: function(bookId, deferred){ var scope = this; $http.get('api/books/' + bookId) .success(funciton(bookData){ var book = scope._retrieveInstance(bookData.id, bookData); deferred.resolve(book); }) .error(function(){ deferred.reject(); }) }, getBook: function(bookId){ var deferred = $q.defer(); var book = this._search(bookId); if(book){ deferred.resove(book); } else { this._load(bookId, deferred); } return deferred.promise; }, loadAllBooks: function(){ var deferred = $q.defer(); var scope = this; $http.get('api/books') .success(function(booksArray){ var books = []; booksArray.forEach(function(bookData){ var book = scope.l_retrieveInstance(bookData.id, bookData); books.push(book); }); deferred.resolve(books); }) .error(function(){ deferred.reject(); }); return deferred.promise; }, setBook: function(bookData){ var scope = this; var book = this._search(bookData.id); if(book){ book.setData(bookData); } else { book = scope._retrieveInstance(bookData); } return book; } }; return booksManager; }]) </pre><div class="contentsignin">Copy after login</div></div> </div> <p>Book service removes the load method. </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;"> app.factory('Book', ['$http', function($http) { function Book(bookData) { if (bookData) { this.setData(bookData): } // Some other initializations related to book }; Book.prototype = { setData: function(bookData) { angular.extend(this, bookData); }, delete: function() { $http.delete('ourserver/books/' + bookId); }, update: function() { $http.put('ourserver/books/' + bookId, this); }, getImageUrl: function(width, height) { return 'our/image/service/' + this.book.id + '/width/height'; }, isAvailable: function() { if (!this.book.stores || this.book.stores.length === 0) { return false; } return this.book.stores.some(function(store) { return store.quantity > 0; }); } }; return Book; }]); </pre><div class="contentsignin">Copy after login</div></div> </div> <p>Now, multiple controllers can use the same booksManager service. </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;"> app.controller('EditableBookController',['$scope', 'booksManager', function($scope, booksManager){ booksManager.getBook(1).then(function(book){ $scope.book = book; }) }]) .controller('BooksListController',['$scope', 'booksManager', function($scope, booksManager){ booksManager.loadAllBooks().then(function(books){ $scope.books = books; }) }])</pre><div class="contentsignin">Copy after login</div></div> </div> <p></p>