Analysis of DOM operation usage in Angularjs _angularjs

Source: Internet
Author: User

The example in this article describes the DOM operational usage in ANGULARJS. Share to everyone for your reference, specific as follows:

The use of Third-party plug-ins in angular is best encapsulated in instructions (directives), and DOM operations are best refactored into instructions.

Avoid using JQuery to manipulate the DOM, including adding element nodes, removing element nodes, getting element content, hiding or displaying elements. You should use directives to implement these actions, and you have to write your own directives if necessary.

If you find it difficult to change your habits, consider removing jQuery from your Web page. Really, the $http service in Angularjs is very powerful, essentially replacing jquery's Ajax functions, and Angularjs embedded a jquery subset of jqlite--'s internal implementations, including common jquery DOM operations, Event bindings, and so on. But that's not to say that you can't use JQuery with Angularjs. If your page is loaded with jquery then Angularjs will take your jquery first, otherwise it will fall back to Jqlite.

The situation where you need to write your own directives is usually when you use a third-party jQuery plugin. Because the plug-in changes the form value outside of ANGULARJS, it is not immediately responsive to Model. For example, we use a lot of jQueryUI DatePicker plug-ins, when you select a date, the plug-in will fill the date string into the input box. View changed, but did not update Model, because $ ('. DatePicker '). DatePicker (); This code is not part of the ANGULARJS management scope. We need to write a directive to get the DOM changes to be updated in the Model immediately.

var directives = Angular.module (' directives ', []);
Directives.directive (' DatePicker ', function () {return
  function (scope, element, attrs) {
    Element.datepicker ( {
      inline:true,
      dateformat: ' dd.mm.yy ',
      onselect:function (datetext) {
        var Modelpath = $ (this). attr (' Ng-model ');
        Putobject (Modelpath, scope, datetext);
        Scope. $apply ();}});


And then introduce this direcitve in HTML.

<input type= "text" DatePicker ng-model= "Myobject.mydatevalue"/>

Plainly directive is in HTML to write custom label attributes, to achieve the role of plug-ins. This declarative syntax extends the HTML.

It is important to note that there is a Angularui project that provides a lot of directive for us to use, including Plug-ins in the Bootstrap framework and other popular UI components based on jQuery. There is also the ngshowcase of http://www.ngnice.com Community contribution. Angularjs's community is active and the ecosystem is sound.

I hope this article will help you to Angularjs program design.

Related Article

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.