Controllers¶
Overview¶
- You can invoke a controller using directive al-ctrl=”controllerName”, also you can declare a top controller in directive al-app=”controllerName”.
- Your controller should be placed in alight.ctrl.controllerName or in scope.$ns.ctrl for a private controller or in global window.
- Your controller can be a simple function or a class.
- If your controller is a class (has a prototype), then Scope’s methods ($scope, $watch, $getValue etc) are available in “this”.
Controller as a function¶
<div al-app="main">
{{myVar}}
</div>
alight.ctrl.main = function(scope) {
scope.myVar = 123;
}
function main(scope) {
scope.myVar = 123;
}
Controller as a class¶
<div al-app="main">
<button al-click="click()">Click</button>
{{name}}
</div>
function main() {
this.name = 'linux';
}
main.prototype.click = function() {
this.name += '!'
}
class main {
constructor() {
this.name = 'linux';
}
click(value) {
this.name += '!'
}
}
class main
constructor: () ->
@.name = 'linux'
click: () ->
@.name += '!'