Do you build things with AMP? Fill out the new AMP Developer Survey!
  • email



amp-bind allows you to add custom interactivity to your pages beyond using AMP's pre-built components.


Import the amp-bind component in the header.

<script async custom-element="amp-bind" src=""></script>

Basic usage

With amp-bind, you can update element attributes and values via bindings. Here we update the hidden attribute based on a state variable called hideGreeting. On button press, we use the AMP.setState() action to update the state.

<div hidden
  [hidden]="hideGreeting">Hello World</div>
<button on="tap:AMP.setState({ hideGreeting: false })">Show greeting</button>

Binding text

You can dynamically change the text value of an element by declaring a binding to the [text] attribute.

Hello World
<div>Hello <span [text]="myText">World</span></div>
<button on="tap:AMP.setState({ myText: 'AMP' })">Change text</button>

Binding CSS classes

You can dynamically change the CSS classes of an element by adding a binding to the [class] attribute.

Hello World
<div class="background-red"
  [class]="myClass">Hello World</div>
<button on="tap:AMP.setState({ myClass: 'background-green' })">Change class</button>

Binding width and height

Basic element properties such as width and height can be updated as well.

<amp-img src=""
<button on="tap:AMP.setState({
                    myImageDimension: {
                      width: 400,
                      height: 200
  Change size

Hiding & showing

This sample toggles the visibility of two divs based on a input selection. AMP provides the hidden attribute, which we use to hide and show the two divs. Some elements, such as the select element, fire events we can use to update state.

<select on="change:AMP.setState({ option: event.value })">
  <option value="0">No selection</option>
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
<div hidden
  [hidden]="option != 1">
  Option 1
<div hidden
  [hidden]="option != 2">
  Option 2

Initializing state

The initial value of an amp-state variable is null. However, bindings are not evaluated on page load, but on subsequent user actions. This can lead to unwanted side effects if amp-state variables are not correctly initialized.

In this sample, both greetings bind to different amp-state variables. One is initialized via an JSON string inside an amp-state element, the other is not. When the user triggers an AMP.setState(...) action, both bindings will be evaluated resulting in the first binding displaying a null value.

1. Hello World
2. Hello World
<amp-state id="myText">
  <script type="application/json">
<div>1. Hello <span [text]="undefinedText">World</span></div>
<div>2. Hello <span [text]="myText">World</span></div>
<button on="tap:AMP.setState({ myText: 'AMP' })">Change state</button>

Push State

AMP.pushState() writes state changes to the history. Navigating back, will restore the previous state. To test this, increase the count and use your browser's back button to decrease the count.

Item 1
<amp-state id="count">
  <script type="application/json">
<div>Item <span [text]="count">1</span></div>
<button on="tap:AMP.pushState({ count: count + 1 })">Increase count</button>

Debounce input events

For text input, it's a good idea to debounce the input using the input-throttled event. For a more in-depth example, see autosuggest.

Hello ?
<amp-state id="name">
  <script type="application/json">
<input id="name-input"
  placeholder="Enter a name"
  on="input-throttled:AMP.setState({ name: event.value })">
<div>Hello <span [text]="name">?</span></div>


amp-bind-macro makes it possible to reuse expressions across different actions.

The circle has an area of 0.
<amp-bind-macro id="circleArea"
  expression="3.14 * radius * radius">
  <input type="number"
    on="input-throttled:AMP.setState({ radius: event.value })">
    The circle has an area of <span [text]="circleArea(radius)">0</span>.
자세한 설명이 필요하신가요?

이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.

Stack Overflow로 이동
설명이 부족한 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub에서 샘플 수정하기