Well, aku ni xla master sgt pasal Angular(just self-taught). Tp since the first time using Angular, aku dh rasa xboleh klu xpakai. Especially bila develop RESTful application. Til the moment, aku dh develop 4 project menggunakan Angular ni. Syiokkk oo… But then, ada beberapa perkara yang aku harap aku tau lebih awal before start pakai Angular.

1) John Papa’s style-guide

Sesiapa yg develop pakai Angular konfem kenal ngan mamat John Papa ni. Recently, dia nyer style-guide has been endorsed by the core team dlm ng-conf. Mamat ni gak dh publish dia nyer style-guide (in collaboration with the core team) kt github:

https://github.com/johnpapa/angular-styleguide

Klu untuk personal project maybe xpenting sgt nk pakai style-guide ni. Tp kalau develop in team, this is a must follow guide utk memudahkan development. Trust me. His style-guide has been adopted by majority and that will ensure easier reference in the future. And so, banyak point dlm post aku lepas ni actually berkisar dari style-guide beliau. Untuk itu, aku belanje video mamat ni kt recent ng-conf.

2) Controller As vs $scope

Okay, this is quite debatable, tp the team encourage the use of controller-as syntax for a reason. Personally, aku lebih suka dgn syntax controller-as ni sebab lagi senang nk baca, lagi nampak macam javascript, n aku dpt buang dependency dengan $scope.

Dulu, maybe aku buat macam ni:

function UserController($http, $scope){
$scope.bindedVar = "yooo";
}

Skarang aku lagi confortable buat ni

function UserController($http){
this.bindedVar = "yooo";
}

…dan dlm template…
<div ng-controller="UserController as vm">
{{vm.bindedVar}}
</div>

There’s few reason why it’s good.

  • Controller lagi senang nk baca
  • Xde dependency dgn $scope, n because of that, kurang temptation utk abuse $scope
  • Force dotted binding dlm template (there’s horror story pasal variable yg xde dotted ni, because of prototyping nature of javascript)

3) Relaxed a bit on the Angular concept

Dulu masa mula2 kenal dengan declarative nature of Angular, n dh pandai buat directive, habis suma benda aku nk buat directive. Benda yang actually simple dlm javascript aku complicate dengan gobs of directive declaration. Oh gosh… dan bila masa nk debug, DAMNN, that was a nightmare. Kalau bende tu lagi senang nk buat dalam javascript biasa, buat pakai javascript biasa. Kalau bende tu involved closely with DOM operation n highly reusable, then opt for the-angular-way. Untuk itu, let me share a great video here.

4) DON’T $http’ing in the controller

Ini mistake aku yg paling besar n nmpaknya aku kene hidup dgn masalah ni. Sekarang controller yg aku buat mostly fat n bulky. Once dh pakai $http dlm controller, you are tempted to perform the logic on the promise block, which is really, really wrong. LEARN about service, TRY code the service & utilize the service. I know that it is a much troublesome things to learn just to code, but it will definitely help you to write a better, testable codes. With $http in the controller, it’s a bizarre experience once your application getting bigger and larger. A nightmare.

5) Dont mix AngularJS dengan jQuery

I know that there’s a few plugin that you love. And the plugin dependent on jQuery. In fact, beribu2 plugin utk web pakai jQuery. But once you dh pakai Angular, try to look for Angular alternative. Kalau xjumpa, rethink balik, is the feature really needed?

I know that actually if done right, Angular can play nicely with jQuery. In fact, Angular ada include jqLite (simplified version of jQuery).

But here’s the story anyway. Previously, aku ada pakai date-picker jQuery. I’ve been using it for multiple project previously without any issue. The comes Angular. The date-picker was initialized with jQuery dan input yg aku bind dengan ng-model wasn’t returning any value because the changes wasn’t picked up by angular. Naively, aku declare watcher utk dptkan value tu until I’ve realized that I have an alternative in Angular.

That’s all aku rasa nk cakap time ni. Happy coding wooo.