angularjs~ Integrated AJAX and service injection

Source: Internet
Author: User

Angularjs is very beautiful, so that a lot of young people and my uncle, its beauty is not only in the writing, but also in the design approach to perfection, with what services injected into what services, so the method is very intuitive, the programmer feels intuitive, the program in the run up as needed to load, This load-on-demand is much better than the full load mode, both in terms of performance and expressiveness.

Ajax Loading Data

$http Service provides a set of Ajax methods to load data, get,post are supported, and $http services are dynamically loaded in angular, called di or IOC in object-oriented

       Angular.module ('todoapp', []). Controller ('realdatacontroller'  , function ($http, $scope) {            varthis;            $http. Get ('/soa/getmenus'). Success (function (data) {                = data;            }). Error (function (data, status, headers, config) {});

The above is a way to load the $http service and use the HTTP service, and the following is a way to use it with an alias, and the code is more streamlined.

Angular.module ('Todoapp', []). Controller ('RealDataController2', ["$http","$scope", function ($h, $s) {varSelf = This; $h.Get('/soa/getmenus'). Success (function (data) {self.datalist=data;        }). Error (Function (data, status, headers, config) {}); }]);

With the program above, we can bind the data to HTML elements.

<DivNg-controller= "Realdatacontroller as real">        <ul>            <Ling-repeat= "Item in Real.datalist">                <span>{Item.} MenuID}}</span>                <span>{Item.} MenuName}}</span>                <span>{Item.} Updatedate | Date: ' Yyyy-mm-dd HH:mm:ss Z '}}</span>                <span><ahref= "javascript:void (0)"Ng-click= "Real.edit ()">Edit</a></span>| <span><ahref= "javascript:void (0)"Ng-click= "Real.del (item)">Delete</a></span>            </Li>        </ul>    </Div>

The results are as follows

It is noteworthy that for the "delete" operation, this example has also been implemented, it is divided into two aspects, a foreground user experience display, the second is the background data deletion, the foreground uses angular's two-way binding technology, the elements of the array object is deleted, the background through the $ Http.post call the corresponding API to delete the real data, the code is as follows

// Delete            function (o) {                1);                 // Ajax request Background API clears real data                $http. Post ("/soa/delmenu?id=" + o.menuid);            }

After using angular for a few days, it feels like it's a bit similar to Knockoutjs, and of course, it's enhanced in function, which is beyond doubt!

angularjs~ Integrated AJAX and service injection

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.