KEMBAR78
Scal`a`ngular - Scala and Angular | ODP
Scal`a`ngularScal`a`ngular
Harsh Sharma Khandal
Software Consultant
Knoldus Software LLP
What is Scal`a`ngular?
● A term composed of Scala and Angular JS
● A relationship, a handshake between Scala and
Angular JS
● Way to build AngularJS based applications in type safe
manner with Scala
● Way to provide the bindings to core AngularJS classes
and functions
● A term composed of Scala and Angular JS
● A relationship, a handshake between Scala and
Angular JS
● Way to build AngularJS based applications in type safe
manner with Scala
● Way to provide the bindings to core AngularJS classes
and functions
Requirements in SBT
● Add the following dependency and resolves in the
sbt file,
"com.greencatsoft" %%%! "scalajs-angular" % "0.8-SNAPSHOT"
resolvers += Resolver.sonatypeRepo("snapshots")
Defining Angular JS app
Scala provides Angular JS app to be defined by
registering the structures and code blocks in two ways,
● By registering the classes
● By registering the singleton objects
Registering the classes
Angular.module("todomvc")
.controller[TodoCtrl]
.directive[TodoItemDirective]
.directive[EscapeDirective]
.directive[FocusDirective]
.filter[StatusFilter]
.factory[TaskServiceProxy.Factory]
Registering the singleton objects
Angular.module("todomvc")
.controller(TodoCtrl)
.directive(TodoItemDirective)
.directive(EscapeDirective)
.directive(FocusDirective)
.filter(StatusFilter)
.factory(TaskServiceProxy.Factory)
Injectable Services
Injectable services can be defined as follows,
● Controllers,
● Factories,
● Directives,
● Filters
Defining a Service
● Any service we define must be inherited from its
parent trait Service,
● In injectable services like, we use the @injectable to
specify the name of service
Services
@injectable("todoUser")
class UserDirective extends AttributeDirective {
...
}
Factory
@injectable("taskService")
class TaskService(http: HttpService) extends Service {
...
}
@injectable("taskService")
class TaskServiceFactory(http: HttpService) extends
Factory[TaskService] {
...
}
Controllers and Scopes
● Controller is a special angular JS service, used to
communicate with template by manipulating a scope
object
● In Scala, scope declares a set of properties we want
to access from controller class
● Controller uses the name of the scope trait as type
parameter of controller or AbstractController class
Scope
trait UserScope extends Scope {
var id: String = js.native
var name: String = js.native
var email: String = js.native
var friends: js.Array[String] = js.native
var delete: js.Function = js.native
}
Controller
@injectable("userDetailsCtrl")
class UserDetailsController(scope: UserScope, http: HttpService)
extends AbstractController[UserScope](scope) {
val future: Future[User] = http.get("/users/john")
Controller with JSExport
@JSExport
@injectable("userDetailsCtrl")
class UserDetailsController(scope: UserScope, http: HttpService)
extends AbstractController[UserScope](scope) {
...
@JSExport
def delete(): Unit = userService.delete(scope.id)
}
<div ng-controller="userDetailsCtrl">
...
<button ng-click="controller.delete()">Delete</button>
</div>
References
● https://github.com/greencatsoft/scalajs-angular
Scal`a`ngular - Scala and Angular

Scal`a`ngular - Scala and Angular