2015-09-04 23:11:31 +00:00
---
layout: page
2017-06-19 10:13:57 +00:00
title: "Backend Angular API in Apache Zeppelin"
2016-08-06 05:50:25 +00:00
description: "Apache Zeppelin provides a gateway between your interpreter and your compiled AngularJS view templates. You can not only update scope variables from your interpreter but also watch them in the interpreter, which is JVM process."
2017-06-19 10:13:57 +00:00
group: usage/display_system
2015-09-04 23:11:31 +00:00
---
2015-11-12 10:54:38 +00:00
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
2015-09-04 23:11:31 +00:00
{% include JB/setup %}
2017-06-19 10:13:57 +00:00
# Backend Angular API in Apache Zeppelin
2015-09-04 23:11:31 +00:00
2016-06-25 19:44:53 +00:00
< div id = "toc" > < / div >
2015-09-04 23:11:31 +00:00
2016-06-25 19:44:53 +00:00
## Overview
2015-09-04 23:11:31 +00:00
2016-06-25 19:44:53 +00:00
Angular display system treats output as a view template for [AngularJS ](https://angularjs.org/ ).
It compiles templates and displays them inside of Apache Zeppelin. Zeppelin provides a gateway between your interpreter and your compiled **AngularJS view** templates.
2016-01-24 11:42:59 +00:00
Therefore, you can not only update scope variables from your interpreter but also watch them in the interpreter, which is JVM process.
2015-09-04 23:11:31 +00:00
2016-06-25 19:44:53 +00:00
## Basic Usage
2016-01-24 11:42:59 +00:00
### Print AngularJS view
2015-09-04 23:11:31 +00:00
2016-01-24 11:42:59 +00:00
To use angular display system, you should start with `%angular` .
2017-06-29 11:13:41 +00:00
< img src = "{{BASE_PATH}}/assets/themes/zeppelin/img/screenshots/display_angular.png" width = "60%" / >
2015-09-04 23:11:31 +00:00
2016-01-24 11:42:59 +00:00
Since `name` is not defined, `Hello {{name}}` will display `Hello` .
> **Please Note:** Display system is backend independent.
2015-09-04 23:11:31 +00:00
< br / >
2016-01-24 11:42:59 +00:00
### Bind / Unbind Variables
2015-09-04 23:11:31 +00:00
2016-01-24 11:42:59 +00:00
Through **ZeppelinContext** , you can bind / unbind variables to **AngularJS view** . Currently, it only works in **Spark Interpreter ( scala )** .
2015-09-04 23:11:31 +00:00
2016-01-24 11:42:59 +00:00
```scala
2015-09-04 23:11:31 +00:00
// bind my 'object' as angular scope variable 'name' in current notebook.
z.angularBind(String name, Object object)
// bind my 'object' as angular scope variable 'name' in all notebooks related to current interpreter.
z.angularBindGlobal(String name, Object object)
// unbind angular scope variable 'name' in current notebook.
2015-11-12 02:47:45 +00:00
z.angularUnbind(String name)
2015-09-04 23:11:31 +00:00
// unbind angular scope variable 'name' in all notebooks related to current interpreter.
2015-11-12 02:47:45 +00:00
z.angularUnbindGlobal(String name)
2015-09-04 23:11:31 +00:00
```
2016-01-24 11:42:59 +00:00
Using the above example, let's bind `world` variable to `name` . Then you can see **AngularJs view** is immediately updated.
2015-09-04 23:11:31 +00:00
2017-06-29 11:13:41 +00:00
< img src = "{{BASE_PATH}}/assets/themes/zeppelin/img/screenshots/display_angular1.png" width = "60%" / >
2015-09-04 23:11:31 +00:00
< br / >
2016-01-24 11:42:59 +00:00
### Watch / Unwatch Variables
2015-09-04 23:11:31 +00:00
2016-01-24 11:42:59 +00:00
Through **ZeppelinContext** , you can watch / unwatch variables in **AngularJs view** . Currently, it only works in **Spark Interpreter ( scala )** .
2015-09-04 23:11:31 +00:00
2016-01-24 11:42:59 +00:00
```scala
2015-09-04 23:11:31 +00:00
// register for angular scope variable 'name' (notebook)
z.angularWatch(String name, (before, after) => { ... })
// unregister watcher for angular variable 'name' (notebook)
z.angularUnwatch(String name)
// register for angular scope variable 'name' (global)
z.angularWatchGlobal(String name, (before, after) => { ... })
// unregister watcher for angular variable 'name' (global)
z.angularUnwatchGlobal(String name)
```
2016-01-24 11:42:59 +00:00
Let's make a button. When it is clicked, the value of `run` will be increased 1 by 1.
2017-06-29 11:13:41 +00:00
< img src = "{{BASE_PATH}}/assets/themes/zeppelin/img/screenshots/display_angular2.png" width = "60%" / >
2016-01-24 11:42:59 +00:00
`z.angularBind("run", 0)` will initialize `run` to zero. And then, it will be also applied to `run` in `z.angularWatch()` .
When the button is clicked, you'll see both `run` and `numWatched` are incremented by 1.
2017-06-29 11:13:41 +00:00
< img src = "{{BASE_PATH}}/assets/themes/zeppelin/img/screenshots/display_angular3.png" width = "60%" / >
2016-01-24 11:42:59 +00:00
## Let's make it Simpler and more Intuitive
2016-04-22 06:37:44 +00:00
In this section, we will introduce a simpler and more intuitive way of using **Angular Display System** in Zeppelin.
2016-01-24 11:42:59 +00:00
2016-04-22 06:37:44 +00:00
Here are some usages.
2016-01-24 11:42:59 +00:00
2016-06-25 19:44:53 +00:00
### Import
2016-01-24 11:42:59 +00:00
```scala
2016-06-25 19:44:53 +00:00
// In notebook scope
2016-01-24 11:42:59 +00:00
import org.apache.zeppelin.display.angular.notebookscope._
import AngularElem._
2016-06-25 19:44:53 +00:00
// In paragraph scope
2016-01-24 11:42:59 +00:00
import org.apache.zeppelin.display.angular.paragraphscope._
import AngularElem._
```
2016-06-25 19:44:53 +00:00
### Display Element
2016-01-24 11:42:59 +00:00
```scala
// automatically convert to string and print with %angular display system directive in front.
2017-05-02 10:03:11 +00:00
< div > < / div > .display
2016-01-24 11:42:59 +00:00
```
2016-06-25 19:44:53 +00:00
### Event Handler
2016-01-24 11:42:59 +00:00
```scala
// on click
< div > < / div > .onClick(() => {
my callback routine
}).display
// on change
< div > < / div > .onChange(() => {
my callback routine
}).display
// arbitrary event
< div > < / div > .onEvent("ng-click", () => {
my callback routine
}).display
```
2015-09-04 23:11:31 +00:00
2016-06-25 19:44:53 +00:00
### Bind Model
2016-01-24 11:42:59 +00:00
```scala
// bind model
< div > < / div > .model("myModel").display
// bind model with initial value
2016-04-22 06:37:44 +00:00
< div > < / div > .model("myModel", initialValue).display
2016-01-24 11:42:59 +00:00
```
2016-06-25 19:44:53 +00:00
### Interact with Model
2016-04-22 06:37:44 +00:00
```scala
2016-01-24 11:42:59 +00:00
// read model
AngularModel("myModel")()
// update model
AngularModel("myModel", "newValue")
```
< br / >
### Example: Basic Usage
2016-04-22 06:37:44 +00:00
Using the above basic usages, you can apply them like below examples.
2016-01-24 11:42:59 +00:00
#### Display Elements
```scala
< div style = "color:blue" >
< h4 > Hello Angular Display System< / h4 >
< / div > .display
```
#### OnClick Event
```scala
< div class = "btn btn-success" >
Click me
< / div > .onClick{() =>
// callback for button click
}.display
```
#### Bind Model
{% raw %}
```scala
< div > {{{{myModel}}}}< / div > .model("myModel", "Initial Value").display
```
{% endraw %}
#### Interact With Model
```scala
// read the value
AngularModel("myModel")()
// update the value
AngularModel("myModel", "New value")
```
2017-06-29 11:13:41 +00:00
< img src = "{{BASE_PATH}}/assets/themes/zeppelin/img/docs-img/basic-usage-angular.png" width = "70%" >
2016-01-24 11:42:59 +00:00
### Example: String Converter
Using below example, you can convert the lowercase string to uppercase.
2016-04-22 06:37:44 +00:00
2016-01-24 11:42:59 +00:00
{% raw %}
```scala
// clear previously created angular object.
AngularElem.disassociate
val button = < div class = "btn btn-success btn-sm" > Convert< / div > .onClick{() =>
val inputString = AngularModel("input")().toString
AngularModel("title", inputString.toUpperCase)
}
< div >
{ < h4 > {{{{title}}}}< / h4 > .model("title", "Please type text to convert uppercase") }
Your text { < input type = "text" > < / input > .model("input", "") }
{button}
< / div > .display
```
{% endraw %}
2015-09-04 23:11:31 +00:00
2017-06-29 11:13:41 +00:00
< img src = "{{BASE_PATH}}/assets/themes/zeppelin/img/docs-img/string-converter-angular.gif" width = "70%" >