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

 

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



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


 

Example:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.