Skip to content

Instantly share code, notes, and snippets.

@mattbucci
Created August 21, 2014 22:08
Show Gist options
  • Save mattbucci/713e2d454933c7106a59 to your computer and use it in GitHub Desktop.
Save mattbucci/713e2d454933c7106a59 to your computer and use it in GitHub Desktop.
Angular Best Practices This is meant as a series of quick snippets to improve code quality

This is a list of angular snippets

There's a lot of guides but they're all quite verbose... see https://google-styleguide.googlecode.com/svn/trunk/angularjs-google-style.html

This is meant to be just a quick reference for improving code legibility

Skinny Controller, Fat Service

Controllers should be simple dumb routers for actions within their scope. They pull in dependencies and allow dom elements to trigger functional calls.

Nest All your controllers within one master controller, This controller can bring in whatever global services you need and assign them to scope

<div ng-app="myapp" ng-controller="AppCtrl">
  <div ng-controller="PageCtrl">
  </div>
</div>

angular.module("myapp")
  .config(...)
  .controller('AppCtrl', [api, config, $scope, function (api,$scope) {
    $scope.api = api;
    $scope.config = config;
  }])

Services Should not use this to assign properties, instead rename this to be the name of the service

.service('Api',[$http, function($http){
  var Api = this;
  
  Api.fetch = function(url) {
    return $http.get(url)
  }
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment