search

Home  >  Q&A  >  body text

angular.js - Is it possible to two-way data bind iframe elements through vue or angular?

I would like to ask you about developing a management system for H5 games. I want to synchronize the content of the page elements in the iframe in real time when inputting content in the background. I would like to know what technology can be used to achieve this? Can vue and angular achieve this?

The effect is as follows:

黄舟黄舟2894 days ago1091

reply all(3)I'll reply

  • 習慣沉默

    習慣沉默2017-05-15 17:16:12

    parent.html

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    <code><p id="app">

        <input type="text" v-model="item.name" />

        <input type="checkbox" v-model="item.check" />

        <input type="date" v-model="item.date" />

        <iframe ref="iframe" src="child.html" @load="load"></iframe>

    </p>

     

    <script src="http://cdn.bootcss.com/vue/2.1.8/vue.min.js"></script>

     

    <script>

        window.app = new Vue({

            el: '#app',

            data() {

                return {

                    item: {

                        name: null,

                        check: false,

                        date: null,

                    },

                }

            },

            methods: {

                load: function (item) {

                    const app = this.$refs.iframe.contentWindow.app;

     

                    if (app && app.setContent) {

                        app.setContent(this.item)

                    }

                    else {

                        window._item = this.item

                    }

                }

            }

        })

    </script></code>


    child.html

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    <code><p id="app">

        {{ item }}

    </p>

     

    <script src="http://cdn.bootcss.com/vue/2.1.8/vue.min.js"></script>

     

    <script>

        window.app = new Vue({

            el: '#app',

            data() {

                return {

                    item: null,

                }

            },

            created: function () {

                this.setContent(window.parent.window._item);

            },

            methods: {

                setContent: function (item) {

                    this.item = item;

                }

            }

        })

    </script></code>


    Or pass the vuex store to use the same store for the vue instances of the two pages

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-15 17:16:12

    If you use iframe, you can only use websocket to notify of updates. The two-way data of vue and ag can only be updated in real time on the same page

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-15 17:16:12

    iframe is cross-page. It is extremely unsafe and impossible for others to use ng or vue to operate your page.

    You can try postMessage.

    reply
    0
  • Cancelreply