AngularJS советы от команды PayPal

Перевод/формат статьи “Sane, scalable Angular apps are tricky, but not impossible. Lessons learned from PayPal Checkout.”

(Очень радует, что на зло всем критикам появляется все больше и больше серьезных приложений на AngularJS)

Continue reading

AngularJS: Зачем котроллеру ngModel нужны $formatters и $parsers

Небольшая заметка о том, для чего нужны $formatters и $parsers в контроллере ngModel директивы и когда их можно применять.

Вкратце:

  • $formatters определяют как модель будет представлена во вью
  • $parsers определяют как значения из вью будет записаны в модель

Continue reading

AngularJS: {{ }} vs ng-bind

I was just curious about this discussion on stackoverflow and decided to do own test:

[javascript]
for(var i = 0; i < 100000; i++){
randomKey = randomString();
bigScope1[randomKey] = randomString();
bigScope2[randomKey] = randomString();
bigNgBindTempalte += ‘<div class="’ + randomString() + ‘" ng-bind="’+ randomKey + ‘"></div>’;
bigExpressionTemplate += ‘<div class="’ + randomString() + ‘">{{‘ + randomKey + ‘}}</div>’;
}

// console.time(‘ngBind’);
// $compile(‘<div>’ + bigNgBindTempalte + ‘</div>’)(bigScope1);
// console.timeEnd(‘ngBind’);

console.time(‘expression’);
$compile(‘<div>’ + bigExpressionTemplate + ‘</div>’)(bigScope2);
console.timeEnd(‘expression’);
[/javascript]

code.

and indeed ngBind is faster,my machine for 100000 iterations shows:

ngBind: 46984.981ms
expression: 51107.555ms

It’s still not clear why the result are so weird when you run it together (one by one). But it’s another question.

AngularJS drag&drop solution

dragula

Dragula is a light weight library(or module) for applying drag&drop functionality on you application. There is also an official AngularJS adapter for it – angular-dragula.

It has quite strange module dependancy declaration:

[javascript]
var app = angular.module(‘my-app’, [angularDragula(angular)]);
[/javascript]

so I put an example on plunker to make it easy for you to start.

 

Keep an eye on AngularJS perfomance with ng-stats

ng-stats

ng-stats is nice utility from Kent C. Dodds that allows you to see statistics for your page’s angular digest/watches.

to install just put this code into your bookmarks:

[javascript]
javascript: (function() {var a = document.createElement("script");a.src = "https://rawgithub.com/kentcdodds/ng-stats/master/dist/ng-stats.js";a.onload=function(){window.showAngularStats()};document.head.appendChild(a)})();
[/javascript]

AngularJS: from 1.2 to 1.4 hints

Just a checklist to prevent issues with application migration.

Expressions

  • no .bind, .call and .apply
  • no __proto__
  • no Object

toBoolean

fixed strange behaviour (from 1.3): before ‘f’, ‘0’, ‘false’, ‘no’, ‘n’, ‘[]’  have been converted to false. Check you ng-if statements.

helpers

.copy() – makes a copy of only own properties (from 1.3)

.forEach() – does not take in account new elements (if during the loop size was changed)(from 1.3)

other

directive property replace – is now deprecated (from 1.3)

responseInterceptors property was removed from $httpProvider (from 1.3) – now it’s just $httpProvider.interceptors

$cookieStore is deprecated from 1.4 ( you should use $cookie instead)

If you know more tricky places – do not hesitate to share!

 

AngularJS: Синтаксис Controller as

Решил выложить пример кода “Controller as” в песочнице, чтобы можно было поиграться и понять, что нельзя миксовать 2 стиля написания контроллеров: либо $scope и писать просто переменные, либо this и делать синтаксис Controller as.