Skip to content

Instantly share code, notes, and snippets.

@icfantv
Created May 6, 2015 16:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icfantv/cc60e677ce01975e1bd6 to your computer and use it in GitHub Desktop.
Save icfantv/cc60e677ce01975e1bd6 to your computer and use it in GitHub Desktop.
ControllerAs Syntax
<html>
<head>
<!-- include angular -->
</head>
<body data-ng-app='app'>
<div data-ng-controller='MyController as MyController'>
<!-- bound once -->
Controller Loaded: {{ ::MyController.someModel.controllerLoaded }}
<hr/>
<!-- two-way binding -->
Name: {{ MyController.someModel.name }}
</div>
</body>
</html>
var app = angular.module('app', []);
app.service('MyService', [MyService]);
app.controller('MyController', ['MyService', MyController]);
function MyService() {
this.serviceFunction = function() {
console.log('in service function');
}
}
function MyController(MyService) {
var vm = this;
vm.someModel = {
id: '123',
name: 'steve',
mood: 'happy',
controllerLoaded: true
};
MyService.serviceFunction();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment