Skip to content
Related Articles

Related Articles

Improve Article

What is Initialize state in amp-bind of Google AMP ?

  • Last Updated : 25 Oct, 2020

 

The amp-bind is made in such a way that any document using it has a mutable JSON data or state. This data can be manipulated using amp-state. Your data is not evaluated on page load but dynamically evaluated along with user input. It has various state and they have their own variable. In this article, we will talk about the initial state of the elements.

The initial of an amp-state variable is always NULL but it can be changed when the user interacts with the page.

Setup: To use amp-bind-macro in our AMP page we have to import amp-bind script in the head of the document.

HTML






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

 

Example:

HTML




<!doctype html>
<html amp>
  
<head>
    <meta charset="utf-8">
    <title>Google AMP amp-bind</title>
  
    <link rel="canonical" href=
  
    <meta name="viewport" content=
"width=device-width,minimum-scale=1,initial-scale=1">
  
    <script async src=
    </script>
  
    <!-- Import amp-bind component in header -->
    <script async custom-element="amp-bind" 
    </script>
  
    <style amp-boilerplate>
        body {
            -webkit-animation: -amp-start 8s 
                steps(1, end) 0s 1 normal both;
              
            -moz-animation: -amp-start 8s 
                steps(1, end) 0s 1 normal both;
              
            -ms-animation: -amp-start 8s 
                steps(1, end) 0s 1 normal both;
              
            animation: -amp-start 8s 
                steps(1, end) 0s 1 normal both
        }
  
        @-webkit-keyframes -amp-start {
            from {
                visibility: hidden
            }
  
            to {
                visibility: visible
            }
        }
  
        @-moz-keyframes -amp-start {
            from {
                visibility: hidden
            }
  
            to {
                visibility: visible
            }
        }
  
        @-ms-keyframes -amp-start {
            from {
                visibility: hidden
            }
  
            to {
                visibility: visible
            }
        }
  
        @-o-keyframes -amp-start {
            from {
                visibility: hidden
            }
  
            to {
                visibility: visible
            }
        }
  
        @keyframes -amp-start {
            from {
                visibility: hidden
            }
  
            to {
                visibility: visible
            }
        }
    </style>
  
    <noscript>
        <style amp-boilerplate>
            body {
                -webkit-animation: none;
                -moz-animation: none;
                -ms-animation: none;
                animation: none
            }
        </style>
    </noscript>
      
    <style amp-custom>
        h1 {
            color: forestgreen;
            text-align: center;
        }
    </style>
</head>
  
<body>
    <div style="padding: 1em; color: crimson;">
        <amp-state id="myText">
            <script type="application/json">
                "Geek"
            </script>
        </amp-state>
        <div>1. Hello <span ="undefinedText">
                Geek</span>
        </div>
        <div>2. Hello <span ="myText">
                Geek</span>
        </div>
        <br>
        <button on="tap:AMP.setState({
                  myText: 'GeeksForGeeks' 
                  })">
            Change state
        </button>
    </div>
</body>
  
</html>

Output:

In the above example, one greeting is bind inside an amp-state via a JSON string. When the user triggers an AMP.setState(…) action, both bindings will be evaluated resulting in the first binding displaying a null value.




My Personal Notes arrow_drop_up
Recommended Articles
Page :