Getting Started with AngularJS 1.5 and ES6: part2
HTTPIn the posts component,the In a real world application,it could be fecthed from external resources,such as third party APIs. Angular service is a good place to do this work. ServiceExtract posts data codes and put into a standalone class called common/services/post.service.js: class Post { constructor() { } getAllPosts(){ return [ { id: 1,title: 'Getting started with REST',content: 'Content of Getting started with REST',createdAt: '9/22/16 4:15 PM' },{ id: 2,title: 'Getting started with AngularJS 1.x',content: 'Content of Getting started with AngularJS 1.x',{ id: 3,title: 'Getting started with Angular2',content: 'Content of Getting started with Angular2',]; } In class PostsController { constructor(Post) { 'ngInject'; this._Post = Post; ... } $onInit() { console.log("initializing Posts..."); this.posts=this._Post.getAllPosts(); } } Note, In order to make this Add the following codes into import PostService from './post.service'; //... let commonServicesModule = angular.module('app.common.services',[]) //... .service('Post',PostService) /... Add commonServices as a dependency of posts module. let postsModule = angular.module('posts',[commonSevices,.... Run the application again and make sure it works as before. Beside Angular service,there are other two facilities existed in Angular: factory and provider. They all can be considered as service,but they are designated for different purposes. factory applies the Factory pattern in codes,should return a new instance for every call. We have used providers in before codes,such as More detailed explanation,read this stackoverflow discussion about difference between service,factory and provider. Next,let's fetch data from remote APIs. HTTPAngular has built-in To demonstrate HTTP interaction,I used another Java EE 7/Jaxrs REST API sample project I created to serve as backend APIs. There are some variants in this repository,we used the cdi one in this project. Follow the Getting started guide to deploy it into a running Wildfly 10.x,it serves several APIs for use.
Configure the api base url in app.constants.js file. const AppConstants = { //... api: 'http://localhost:8080/blog-api-cdi/api' }; //... Inject class Post { constructor(AppConstants,$http,$q) { 'ngInject'; this._AppConstants = AppConstants; this._$http = $http; this._$q = $q; } query(keyword) { let deferred = this._$q.defer(); // Create the $http object for this request let request = { url: this._AppConstants.api + '/posts',method: 'GET',params: !!keyword ? { 'q': keyword } : null }; this._$http(request) .then( (res) => deferred.resolve(res.data),(err) => deferred.reject(err) ); return deferred.promise; } export default Post; The GET Modify posts controller class to use $onInit() { console.log("initializing Posts..."); this.searchPosts(); } searchPosts() { this._Post .query(this.q) .then( (res) => this.posts = res ); } Now run the applicaiton again,everything should be working as expected. Check the sample codes. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |