Appended html element inside angular directive is getting removed from DOM on changing view










1















I am developing a support chat system. Whenever a support staff clicks on support icon, support screen (staff-room.html) opens which has list of client waiting for request to be attended. Following is an extract of code from the above file



<md-list-item ng-repeat="client in clientChannelList | filter: searchText">
<div class="channel" layout="row" layout-align="center center">
<div class="md-list-item-text" layout="column" layout-align="center start" flex>
<h3 class="name"> client.senderName</h3>
<span class="site-name"> client.siteName</span>
</div>
</div>
<button chat-popup-box>Attend</button>
</md-list-item>

<div flex layout="column">
<md-toolbar class="background-transparent">
<div class="md-toolbar-tools" layout="row"></div>
</md-toolbar>
<md-divider></md-divider>
<div id="chat-popup"></div>
<md-divider></md-divider>
</div>


I have an attribute directive chat-popup-box (present in button element) which initialises on clicking of a button. Inside this directive I am creating an html element which creates chat pop up window and appending it with element having id chat-popup in the parent (staff-room.html).



var htmlelement = `<div class="popup-box chat-popup">Some stuff</div>`
var compiledElement = $compile(htmlelement)($scope);
var pageElement = angular.element(document.getElementById("chat-popup"));
pageElement.append(compiledElement);


Now the problem is whenever I am changing the state(navigating to any other module) and coming back, I am loosing the HTML which I have appended in <div id="chat-popup"></div> from DOM. Hence I am loosing chat window pop up.
So Before changing state view DOM was something like this



<div id="chat-popup">
<div class="popup-box chat-popup">Some stuff</div>
</div>


But after changing state and coming back to same state DOM is like this



<div id="chat-popup"></div>


While debugging I found out that directive is not being destroyed as even after changing state, flow was going inside the chat-popup-box directive but HTML which I have appended is getting removed from DOM. Is there a way to prevent that?










share|improve this question




























    1















    I am developing a support chat system. Whenever a support staff clicks on support icon, support screen (staff-room.html) opens which has list of client waiting for request to be attended. Following is an extract of code from the above file



    <md-list-item ng-repeat="client in clientChannelList | filter: searchText">
    <div class="channel" layout="row" layout-align="center center">
    <div class="md-list-item-text" layout="column" layout-align="center start" flex>
    <h3 class="name"> client.senderName</h3>
    <span class="site-name"> client.siteName</span>
    </div>
    </div>
    <button chat-popup-box>Attend</button>
    </md-list-item>

    <div flex layout="column">
    <md-toolbar class="background-transparent">
    <div class="md-toolbar-tools" layout="row"></div>
    </md-toolbar>
    <md-divider></md-divider>
    <div id="chat-popup"></div>
    <md-divider></md-divider>
    </div>


    I have an attribute directive chat-popup-box (present in button element) which initialises on clicking of a button. Inside this directive I am creating an html element which creates chat pop up window and appending it with element having id chat-popup in the parent (staff-room.html).



    var htmlelement = `<div class="popup-box chat-popup">Some stuff</div>`
    var compiledElement = $compile(htmlelement)($scope);
    var pageElement = angular.element(document.getElementById("chat-popup"));
    pageElement.append(compiledElement);


    Now the problem is whenever I am changing the state(navigating to any other module) and coming back, I am loosing the HTML which I have appended in <div id="chat-popup"></div> from DOM. Hence I am loosing chat window pop up.
    So Before changing state view DOM was something like this



    <div id="chat-popup">
    <div class="popup-box chat-popup">Some stuff</div>
    </div>


    But after changing state and coming back to same state DOM is like this



    <div id="chat-popup"></div>


    While debugging I found out that directive is not being destroyed as even after changing state, flow was going inside the chat-popup-box directive but HTML which I have appended is getting removed from DOM. Is there a way to prevent that?










    share|improve this question


























      1












      1








      1








      I am developing a support chat system. Whenever a support staff clicks on support icon, support screen (staff-room.html) opens which has list of client waiting for request to be attended. Following is an extract of code from the above file



      <md-list-item ng-repeat="client in clientChannelList | filter: searchText">
      <div class="channel" layout="row" layout-align="center center">
      <div class="md-list-item-text" layout="column" layout-align="center start" flex>
      <h3 class="name"> client.senderName</h3>
      <span class="site-name"> client.siteName</span>
      </div>
      </div>
      <button chat-popup-box>Attend</button>
      </md-list-item>

      <div flex layout="column">
      <md-toolbar class="background-transparent">
      <div class="md-toolbar-tools" layout="row"></div>
      </md-toolbar>
      <md-divider></md-divider>
      <div id="chat-popup"></div>
      <md-divider></md-divider>
      </div>


      I have an attribute directive chat-popup-box (present in button element) which initialises on clicking of a button. Inside this directive I am creating an html element which creates chat pop up window and appending it with element having id chat-popup in the parent (staff-room.html).



      var htmlelement = `<div class="popup-box chat-popup">Some stuff</div>`
      var compiledElement = $compile(htmlelement)($scope);
      var pageElement = angular.element(document.getElementById("chat-popup"));
      pageElement.append(compiledElement);


      Now the problem is whenever I am changing the state(navigating to any other module) and coming back, I am loosing the HTML which I have appended in <div id="chat-popup"></div> from DOM. Hence I am loosing chat window pop up.
      So Before changing state view DOM was something like this



      <div id="chat-popup">
      <div class="popup-box chat-popup">Some stuff</div>
      </div>


      But after changing state and coming back to same state DOM is like this



      <div id="chat-popup"></div>


      While debugging I found out that directive is not being destroyed as even after changing state, flow was going inside the chat-popup-box directive but HTML which I have appended is getting removed from DOM. Is there a way to prevent that?










      share|improve this question
















      I am developing a support chat system. Whenever a support staff clicks on support icon, support screen (staff-room.html) opens which has list of client waiting for request to be attended. Following is an extract of code from the above file



      <md-list-item ng-repeat="client in clientChannelList | filter: searchText">
      <div class="channel" layout="row" layout-align="center center">
      <div class="md-list-item-text" layout="column" layout-align="center start" flex>
      <h3 class="name"> client.senderName</h3>
      <span class="site-name"> client.siteName</span>
      </div>
      </div>
      <button chat-popup-box>Attend</button>
      </md-list-item>

      <div flex layout="column">
      <md-toolbar class="background-transparent">
      <div class="md-toolbar-tools" layout="row"></div>
      </md-toolbar>
      <md-divider></md-divider>
      <div id="chat-popup"></div>
      <md-divider></md-divider>
      </div>


      I have an attribute directive chat-popup-box (present in button element) which initialises on clicking of a button. Inside this directive I am creating an html element which creates chat pop up window and appending it with element having id chat-popup in the parent (staff-room.html).



      var htmlelement = `<div class="popup-box chat-popup">Some stuff</div>`
      var compiledElement = $compile(htmlelement)($scope);
      var pageElement = angular.element(document.getElementById("chat-popup"));
      pageElement.append(compiledElement);


      Now the problem is whenever I am changing the state(navigating to any other module) and coming back, I am loosing the HTML which I have appended in <div id="chat-popup"></div> from DOM. Hence I am loosing chat window pop up.
      So Before changing state view DOM was something like this



      <div id="chat-popup">
      <div class="popup-box chat-popup">Some stuff</div>
      </div>


      But after changing state and coming back to same state DOM is like this



      <div id="chat-popup"></div>


      While debugging I found out that directive is not being destroyed as even after changing state, flow was going inside the chat-popup-box directive but HTML which I have appended is getting removed from DOM. Is there a way to prevent that?







      javascript angularjs dom angularjs-directive






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 5:05







      ot954

















      asked Nov 15 '18 at 18:58









      ot954ot954

      7010




      7010






















          1 Answer
          1






          active

          oldest

          votes


















          0














          A good start might be to code your page in a more AngularJS-y way. The idea is that values are stored and manipulated in the various scope objects and component controllers. I'm not sure which part of the html is replaced, in your case, when you change to another module, but if you stay within the same app, your html will be re-rendered, including your button and functionality, but the popup will not appear until you press the button (again). If you want the popup to be open the next time you arrive on this page, you will have to store this fact in code somewhere. In AngularJS, services are typically used for this purpose.



          You could do something like this:



          <md-list-item ng-repeat="client in clientChannelList | filter: searchText">
          (...)
          <button ng-click="controller.changeActiveClient()">Attend</button>
          </md-list-item>

          <div flex layout="column">
          (...)
          <div id="chat-popup">
          <div class="popup-box chat-popup" ng-if="controller.activeClient">Some stuff</div>
          </div>
          <md-divider></md-divider>
          </div>


          (better yet, create a component to hold your popup box)



          Then, in your controller have something like this:



          this.activeClient = staffRoomService.getActiveClient();
          this.setActiveClient = staffRoomService.setActiveClient;


          Or something; then, create the staffRoomService to store your active client. Anyway, do read up on scopes, components, and "the AngularJS way". It will make your life better and more fun. https://docs.angularjs.org/guide/concepts






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326224%2fappended-html-element-inside-angular-directive-is-getting-removed-from-dom-on-ch%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            A good start might be to code your page in a more AngularJS-y way. The idea is that values are stored and manipulated in the various scope objects and component controllers. I'm not sure which part of the html is replaced, in your case, when you change to another module, but if you stay within the same app, your html will be re-rendered, including your button and functionality, but the popup will not appear until you press the button (again). If you want the popup to be open the next time you arrive on this page, you will have to store this fact in code somewhere. In AngularJS, services are typically used for this purpose.



            You could do something like this:



            <md-list-item ng-repeat="client in clientChannelList | filter: searchText">
            (...)
            <button ng-click="controller.changeActiveClient()">Attend</button>
            </md-list-item>

            <div flex layout="column">
            (...)
            <div id="chat-popup">
            <div class="popup-box chat-popup" ng-if="controller.activeClient">Some stuff</div>
            </div>
            <md-divider></md-divider>
            </div>


            (better yet, create a component to hold your popup box)



            Then, in your controller have something like this:



            this.activeClient = staffRoomService.getActiveClient();
            this.setActiveClient = staffRoomService.setActiveClient;


            Or something; then, create the staffRoomService to store your active client. Anyway, do read up on scopes, components, and "the AngularJS way". It will make your life better and more fun. https://docs.angularjs.org/guide/concepts






            share|improve this answer



























              0














              A good start might be to code your page in a more AngularJS-y way. The idea is that values are stored and manipulated in the various scope objects and component controllers. I'm not sure which part of the html is replaced, in your case, when you change to another module, but if you stay within the same app, your html will be re-rendered, including your button and functionality, but the popup will not appear until you press the button (again). If you want the popup to be open the next time you arrive on this page, you will have to store this fact in code somewhere. In AngularJS, services are typically used for this purpose.



              You could do something like this:



              <md-list-item ng-repeat="client in clientChannelList | filter: searchText">
              (...)
              <button ng-click="controller.changeActiveClient()">Attend</button>
              </md-list-item>

              <div flex layout="column">
              (...)
              <div id="chat-popup">
              <div class="popup-box chat-popup" ng-if="controller.activeClient">Some stuff</div>
              </div>
              <md-divider></md-divider>
              </div>


              (better yet, create a component to hold your popup box)



              Then, in your controller have something like this:



              this.activeClient = staffRoomService.getActiveClient();
              this.setActiveClient = staffRoomService.setActiveClient;


              Or something; then, create the staffRoomService to store your active client. Anyway, do read up on scopes, components, and "the AngularJS way". It will make your life better and more fun. https://docs.angularjs.org/guide/concepts






              share|improve this answer

























                0












                0








                0







                A good start might be to code your page in a more AngularJS-y way. The idea is that values are stored and manipulated in the various scope objects and component controllers. I'm not sure which part of the html is replaced, in your case, when you change to another module, but if you stay within the same app, your html will be re-rendered, including your button and functionality, but the popup will not appear until you press the button (again). If you want the popup to be open the next time you arrive on this page, you will have to store this fact in code somewhere. In AngularJS, services are typically used for this purpose.



                You could do something like this:



                <md-list-item ng-repeat="client in clientChannelList | filter: searchText">
                (...)
                <button ng-click="controller.changeActiveClient()">Attend</button>
                </md-list-item>

                <div flex layout="column">
                (...)
                <div id="chat-popup">
                <div class="popup-box chat-popup" ng-if="controller.activeClient">Some stuff</div>
                </div>
                <md-divider></md-divider>
                </div>


                (better yet, create a component to hold your popup box)



                Then, in your controller have something like this:



                this.activeClient = staffRoomService.getActiveClient();
                this.setActiveClient = staffRoomService.setActiveClient;


                Or something; then, create the staffRoomService to store your active client. Anyway, do read up on scopes, components, and "the AngularJS way". It will make your life better and more fun. https://docs.angularjs.org/guide/concepts






                share|improve this answer













                A good start might be to code your page in a more AngularJS-y way. The idea is that values are stored and manipulated in the various scope objects and component controllers. I'm not sure which part of the html is replaced, in your case, when you change to another module, but if you stay within the same app, your html will be re-rendered, including your button and functionality, but the popup will not appear until you press the button (again). If you want the popup to be open the next time you arrive on this page, you will have to store this fact in code somewhere. In AngularJS, services are typically used for this purpose.



                You could do something like this:



                <md-list-item ng-repeat="client in clientChannelList | filter: searchText">
                (...)
                <button ng-click="controller.changeActiveClient()">Attend</button>
                </md-list-item>

                <div flex layout="column">
                (...)
                <div id="chat-popup">
                <div class="popup-box chat-popup" ng-if="controller.activeClient">Some stuff</div>
                </div>
                <md-divider></md-divider>
                </div>


                (better yet, create a component to hold your popup box)



                Then, in your controller have something like this:



                this.activeClient = staffRoomService.getActiveClient();
                this.setActiveClient = staffRoomService.setActiveClient;


                Or something; then, create the staffRoomService to store your active client. Anyway, do read up on scopes, components, and "the AngularJS way". It will make your life better and more fun. https://docs.angularjs.org/guide/concepts







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 9:16









                Spiny NormanSpiny Norman

                7,39112350




                7,39112350





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326224%2fappended-html-element-inside-angular-directive-is-getting-removed-from-dom-on-ch%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3