David Cai | Blog

Posts in “Angular”

Tips on Promise and $http Service

Mon, Sep 14, 2015

Promise

I’ve been reviewing a lot of Angular code lately. One common problem I found is the unnecessary use of deferred object to resolve and reject promises. In this post, I will illustrate a simplified approach. Another problem is a mix of then, catch, two callbacks in then, success, and error. I will go through each, and propose adopting a single style that can also be applied when using other Promise libraries.

Router: Dynamic Templates

Sat, Aug 15, 2015

Dynamic templates

This post discusses how to create dynamic templates by leveraging the templateProvider configuration provided by Angular’s built-in router or the third-party UI Router.

Directive Isolated Scope: @ vs. =

Thu, Aug 13, 2015

@ vs. =

Shall I use @ or = to bind scope variables? Their difference is very well explained in Angular’s documentation. However, this question still often pops up when I create directives. In this post, I listed some of the major differences between these two.

Angular Factory Pattern

Wed, Aug 5, 2015

Factory

Suffix Factory with ‘Service’ The terminology “Factory” in Angular is different from the one used in the Java community. From a Java developer’s perspective, a factory is to create something by a specification supplied by the factory caller. There should be only one public method on a factory - getInstance(). Well, maybe some variations of the getInstance method. But that should be it. Factories simply create stuff. However, in Angular, unlike angular.controller which creates a controller, what angular.factory creates is not a factory but a service - a place to host public APIs and encapsulate logics, not a factory that creates stuff.

Angular Best Practice Recap

Sat, Aug 1, 2015

Best practice

I attended a great Angular training provided by OasisDigital the last week. Learned so many tips and best practices from Bill Odom and his team. While my memory is still fresh, I’d like to document the stuff I learned. So again, I use my blog as study notes. While I’m on the topic of Angular best practices, I also like to bring in some advices from John Papa, Shai Reznik, and other wisdoms of the community.