Integration of jQuery Mobile + AngularJs experience

Source: Internet
Author: User

Both are good JS programming frameworks, but their respective uses are different.

1. jQuery Mobile provides a good graphic space and relies on jQuery itself, providing APIs for a lot of space operations

2. Angular does not have any controls, but it is a good js mvc Framework and provides the space data binding function.

As a result, some open-source projects integrate the two, such

Https://github.com/opitzconsulting/jquery-mobile-angular-adapter

But with this adapter, can the two work well? NO. There are some problems. For example, the loading method of the two pages is different. For each page, Angular uses route to load the page fragments to the browser as needed. The URL is similar. Then jQuery Mobile needs to load all pages to the client at one time. In this case, if you define an angular controller for each page in jQuery Mobile, the page is not displayed when you initialize your angular controller.

We need a design implementation to define an Angular controller for each jQuery Mobile Page. Each controller binds the data of the corresponding page. How can we achieve this?

1. jQuery Mobile is used for page development. We define a root angular controller for the root body, for example, <body ng-controller = "AppCtrl">

2. Navigation problem: the navigation uses $. Mobile. changePage of jQuery mobile instead of angular route, because the page is written by jQuery Mobile. In addition to page switching, the most important thing in page navigation is to initialize page data, because under jQuery mobile, All controllers in the page loading phase are initialized, you can only refresh the bound data of the displayed page in the root controller when navigating. This requires that the binding data of all pages be defined in the root controller according to angular rules, all sub-controllers inherit the data definitions. It is best to use the structure instead of the primary type when defining it, so that the controller can directly consume it, such as defining the data structure of a shop.

 
 
  1. $scope.shop&nbsp;=&nbsp;{};  
  2. $scope.shop.catelogs&nbsp;=&nbsp;null;  
  3. $scope.shop.advices&nbsp;=&nbsp;[];  
  4. $scope.shop.child&nbsp;=&nbsp;[];  
  5. $scope.shop.products&nbsp;=&nbsp;[]; 

3. I often define a separate controller in a navigation bar and use it to initialize the page controller, such

 
 
  1. angular  
  2. .module(  
  3. 'ngPageNav',  
  4. [],  
  5. [  
  6. '$provide',  
  7. function($provide)&nbsp;{  
  8. $provide  
  9. .factory(  
  10. 'ngPageNavigator',  
  11. [  
  12. '$http',  
  13. function($http)&nbsp;{  
  14. function&nbsp;nav($scope,  
  15. $http,&nbsp;action,  
  16. data,  
  17. ignoreStack)&nbsp;{  
  18. .....  
  19. }  
  20. return&nbsp;{  
  21. nav&nbsp;:&nbsp;nav,  
  22. back&nbsp;:&nbsp;back  
  23. };  
  24. &nbsp;  
  25. }  
  26. }&nbsp;]); 

}]). Value ('name', 'ngpagenav ');
 

3. When you call $. mobile. changePage, the page Binding data changes, but the page does not refresh the page. The simplest way is to send a dummy request to the background.

4. If you need to directly switch to a page when opening your jQuery Mobile, listen to jqmInit in your root controller.

 
 
  1. $scope.$on("jqmInit",function() {   
  2.  
  3. }  

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.