AngularJS和Ionic应用性能优化实践与技巧
angular 性能优化
- 使用$watchCollection(obj, listener), 
 不要使用- $watch()或者- $watchGroup();
- 使用 one-time binding - 1 - {{::user.first_name}} 
- 使用 Track by 
 以前的用法- ng-repeat="user in users"
 修改后的用法- ng-repeat="user in users track by user.id"如果 users 有 id 的话
 或者- ng-repeat="user in users track by $index"如果没有 id
- 不要使用 console.log(),而是用$log - The - $logservice has several log levels- .info- .debugand- .error.
- 禁用 dubug - 1 
 2
 3
 4
 5
 6- angular.module('yourModule').config(function($compileProvider) { 
 if (/* test if in production */) {
 $compileProvider.debugInfoEnabled(false);
 }
 });- AngularJS by default adds scope references to the DOM for tools such as angularjs-batarang to work. This has an impact on your application performance. 
- 使用 lodash 库的_.forEach 遍历,他的效率是 angular foreach 的 4 倍。 
ionic 性能优化
- 使用原生滚动方式
| 1 | angular.module("yourModule").config(function ($ionicConfigProvider) { | 
 参考:http://blog.ionic.io/native-scrolling-in-ionic-a-tale-in-rhyme/
- 使用 collection repeat
| 1 | <ion-content> | 
参考:http://ionicframework.com/docs/api/directive/collectionRepeat/
- 无限滚动
| 1 | <ion-content ng-controller="MyController"> | 
使用 one-time binding,track by 和 native scrolling 组合有最好的性能。
- 缓存试图 - 1 
 2
 3- angular.module("yourModule").config(function ($ionicConfigProvider) { 
 $ionicConfigProvider.views.maxCache(5);
 });- 1 
 2
 3
 4
 5- $stateProvider.state("myState", { 
 cache: false,
 url: "/myUrl",
 templateUrl: "my-template.html",
 });- 1 - <ion-view cache-view="false"></ion-view> 
- 试图缓存事件 - 1 
 2
 3
 4
 5
 6
 7
 8- $scope.$on("$ionicView.loaded", function () {}); 
 $scope.$on("$ionicView.enter", function () {});
 $scope.$on("$ionicView.leave", function () {});
 $scope.$on("$ionicView.beforeEnter", function () {});
 $scope.$on("$ionicView.beforeLeave", function () {});
 $scope.$on("$ionicView.afterEnter", function () {});
 $scope.$on("$ionicView.afterLeave", function () {});
 $scope.$on("$ionicView.unloaded", function () {});- 正确使用事件加载数据可以优化程序 










