Skip to content

Macro Enhancements

jokade edited this page Jan 9, 2015 · 1 revision

Macro-based enhancements

Controllers

Module.controllerOf[NGController]

Scala classes extending Controller or ScopeController can be used directly as AngularJS controllers. Both controller flavors support constructor dependency injection.

Controller

Classes extending Controller export all public members to the scope that is available in the template. They must be used with the AngularJS controller as syntax:

package example

// dependency $http is injected on instantiation
class UserCtrl($http: HttpService) extends Controller {
  // available in template scope
  var users: js.Array[User] = fetchUsers().value[js.Array[User]]
  // available in template scope
  def reload() : Unit = fetchUsers().onSuccess( res => users = res.asInstanceOf[js.Array[User]] )
  // not exported to template scope
  private def fetchUsers() = $http.get("/users")
}

val module = Angular.module("app", Nil)

// registers the controller under the fully qualified class name
// i.e. "example.UserCtrl" 
module.controllerOf[UserCtrl]

// registers the controller under "UserAdminCtrl"
module.controllerOf[UserCtrl]("UserAdminCtrl")
<!-- classes extendning Controller must be instantiated with "controller as" -->
<div ng-controller="UserAdminCtrl as ctrl">
  <ul>
    <li ng-repeat="user in ctrl.users">{{user.name}}</li>
  </ul>
</div>

Dependency Injection

Clone this wiki locally