ANGULARJS Application Skeleton (4)

Source: Internet
Author: User

Continue to the previous article to continue to learn about angular other content.

Interacting with the server

Real applications need to interact with real-world servers and the new Chrome desktop apps may be the exception. But for all other applications, whether you want to persist data to the cloud or require other users to interact in real-time, you need to have your app interact with the server.

To achieve this, angular provides a service called $http, which provides an extensible list of abstract methods, making it easier to interact with the server. It supports HTTP, Jsonp, and cors, and it also includes security support that avoids the fragility of JSON format and cross-site request forgery (XSRF). It allows you to easily convert requests and corresponding data, and even implement a simple cache. For example, we intend to have the shopping site get the item list information from the server instead of the memory fake data. For example, we have written a daemon that returns a JSON-formatted data:

  

1 [2   {3"id": 0,4"title": "Badminton",5"description": "Li-ning Badminton, wear-resistant, lightweight",6"Price": 3.957   },8   {9"id": 1,Ten"title": "Badminton Racket", One"description": "Li Ning Badminton Racket N77 second generation, wear-resistant, light", A"Price": 1200 -   }, -   { the"ID": 2, -"title": "Badminton Xie", -"description": "Lex badminton shoes, wear-resistant, lightweight", -"Price": 300 +   } -]
View Code

We can write the query code as follows:

  

1! (function () {2    varApp=angular.module (' Myhttpmodule ',[]);3App.controller (' Shoppingcontroller ',function($scope, $http) {4$http. Get (' Data/data1.json '). Success (function(data,status,headers,config) {5$scope. items=data;6}). Error (function(e) {7 alert (e)8         });9     });Ten}());
View Code

Then use it in the template:

  

1 <!DOCTYPE HTML>2 <HTMLNg-app= "Myhttpmodule">3 <Head>4     <MetaCharSet= "UTF-8">5     <Scriptsrc= "Static/js/angular.js"type= "Text/javascript"></Script>6     <Scriptsrc= "Static/app/controller/controller.js"type= "Text/javascript"></Script>7 </Head>8 <Body>9     <DivNg-controller= "Shoppingcontroller">Ten             <Divng-repeat= "Item in Items"> One                 <P>{{Item.title}}</P> A                 <P>{{Item.description}}</P> -                 <P>{{item.price | currency}}</P> -             </Div> the     </Div> - </Body> - </HTML>
View Code

In the following series, we are talking about this structure and the various functions of the $http service.

  

Modifying the DOM with directives

Directives extend the HTML syntax and are also a way of associating behavior and DOM transformations with custom elements and attributes. With instructions, you can create a UI component that you can take, configure your app, and do almost anything you can imagine, all of which can be done in your template.

You can use the angular built-in instructions to write your app, but there are situations where you have to write your own instructions. When you want to handle browser events in some way or modify the DOM without the built-in instructions to support it, you'll know that it's time to dig deeper into the instruction architecture, and the code you write will be in the instructions you write, not the controller, the service, or the rest of the app.

As with services, you can define instructions through a module, just call the directive () function of the module instance.

var app=angular.module (' myApp ', []);

App.directive (' name ', directivefunction);d irectivefunction is an instruction factory function.

  

Autofocus is a very useful HTML5 property, but how can a wife who does not support HTML5 get the focus automatically when the page is opened? Well, then we're going to implement such an instruction Ng-auto

Template page:

  

 1  <  div  ng-controller  = "Autofoucecontroller"  >  2   ng-auto ng-click   >  3   {{Data.info}}  4   Button  >  5  </ div  >  
View Code

  

Script:

  

1App.controller (' Autofoucecontroller ',function($scope) {2         vardata = {3Info: "hello,angular!"4         };5$scope. Data =data;6$scope. ClickMe =function () {7$scope. Data.info = "hello,world!";8         };9     });Ten  OneApp.directive (' Ngauto ',function () { A         return { -Restrict: ' A ', -Linkfunction(scope, element, Attrs, controller) { theElement[0].focus (); -             } -         }; -});
View Code

  

:

  

In the future serial, will be detailed chat $http, $directive.

  

  

ANGULARJS Application Skeleton (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.