← Home
🔥0 DAY
0 XP
Coding Test · AngularJS 1.x

ANGULARJS
CODING TEST.

Ten questions an AngularJS interviewer or take-home test will ask: digest timing, directive lifecycle, DI, $http cancellation, and ui-router resolves. Read, then run the timed drills to grade yourself.

Scopes & Digest

  1. An async callback mutates $scope but the view doesn't update. Fix it.

    AngularJS only re-renders when a digest runs. Setting $scope from outside Angular (setTimeout, jQuery event, third-party SDK) leaves the digest unaware. Wrap the mutation in $scope.$apply() — or use $timeout / $http which schedule a digest for you.

    setTimeout(function () {
      $scope.$apply(function () {
        $scope.user = data;
      });
    }, 500);
    → Practice this topic
  2. What's the difference between $watch, $watchCollection, and $watchGroup?

    $watch does reference equality (or deep with a third arg). $watchCollection is a shallow check on array / object keys — cheaper than a deep watch when you only care about add/remove. $watchGroup fires once per digest for any change across a list of expressions, with the new + old arrays.

    → Practice this topic

Directives

  1. Difference between compile, controller, pre-link, and post-link?

    Order: compile (template manipulation, runs once per template) → controller (DI-injected, runs per instance) → pre-link (parent before children) → post-link (children before parent — the default 'link' you write). Mutate DOM in post-link; transform the template in compile.

    → Practice this topic
  2. Scope: true vs scope: {} vs scope: false — when do you pick which?

    false = share parent scope (cheap, but reads/writes pollute). true = new child scope that prototypically inherits (good for widgets that read parent state). {} = isolate scope (best for reusable components — explicit bindings with @, =, &).

    → Practice this topic

Services & DI

  1. factory vs service vs provider?

    factory: returns whatever you return — usually an object literal. service: NEW's your constructor (this. members become the API). provider: configurable at module.config() time via a $get() — use only when you need config-phase setup like an API base URL.

    → Practice this topic
  2. Why does minification break my AngularJS app?

    AngularJS infers dependencies from parameter names. Minifiers rename them to a, b, c. Use the inline array annotation: app.controller('X', ['$scope', '$http', function ($scope, $http) { ... }]) or ng-annotate to add it automatically.

    → Practice this topic

HTTP & Promises

  1. How do you cancel an in-flight $http request?

    Pass a timeout that's a $q.defer().promise; resolve the deferred to abort. New code can also pass an AbortController-style canceller via the request config timeout property.

    var canceller = $q.defer();
    $http.get('/api/search', { timeout: canceller.promise });
    // later
    canceller.resolve();
    → Practice this topic
  2. What's the right way to attach a global auth header to every request?

    Write an $http interceptor with a request function that adds the header, register it in $httpProvider.interceptors during config phase. Don't monkey-patch $http or set defaults from a controller — interceptors are the only place that runs for every call.

    → Practice this topic

Routing

  1. ngRoute vs ui-router — which would you pick?

    ui-router for any non-trivial app — it supports nested + named views, state-based navigation, and proper resolve cascades. ngRoute is fine for a single-level URL → template app, nothing more.

    → Practice this topic
  2. What does resolve do in a route definition?

    resolve returns a map of promises that must settle before the route's controller instantiates. Use it to preload required data so the view never flashes empty; the resolved values are injected into the controller by name.

    → Practice this topic