Search code examples
javascriptangularjstwitter-bootstrapangularjs-routing

Editing object with modal jhipster


I used JHipster to create my project and it has been pretty smooth sailing of late. However, I ran into an issue with editing objects on my main.html. I am able to successfully edit the post when I'm on the posts.html page that the router points to, but I am unable to do it with the exact same code on my main.html. I am able to call the information from the postController on my main page, and it is displayed adequately. I am, however, unable to make my data appear inside the modal popup. I know the information is being received by the controller because my console.log($scope.post) returns the object that I want to edit. However, that information is not transmitted to my modal. Any help appreciated.

Button that calls edit:

//inside postController and an ng-repeat of posts calling update function
<button class="btn btn-primary btn-xs" type="submit" ng-click="update(post.id)" class="btn">
    <span class="glyphicon glyphicon-pencil"></span> Edit
</button>

Actual Modal Code:

<div ng-controller="postController">
            <textarea class="form-control" rows="3" placeholder="Status update here..." data-toggle="modal" data-target="#savePostModal" ng-click="clear()"></textarea>
            <div class="modal fade" id="savePostModal" tabindex="-1" role="dialog" aria-labelledby="myPostLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <form name="form" role="form" novalidate class="ng-scope ng-invalid ng-invalid-required ng-dirty ng-valid-minlength" ng-submit="create()">

                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true" ng-click="clear()">&times;</button>
                                <h4 class="modal-title" id="myPostLabel">Create or edit a Post</h4>
                            </div>
                            <div class="modal-body">
                                <div class="form-group">
                                    <label>ID</label>
                                    <input type="text" class="form-control" name="id" ng-model="post.id" readonly>
                                </div>

                                <div class="form-group">
                                    <label>Title</label>
                                    <input type="text" class="form-control" name="test" placeholder="Title your post..." ng-model="post.name" ng-minlength=1 required>
                                </div>

                                <div class="form-group">
                                    <label>Author</label>
                                    <input type="text" class="form-control" name="test" placeholder="Temporary Field..." ng-model="post.author" ng-minlength=1 required>
                                </div>

                                <div class="form-group">
                                    <label>Text</label>
                                    <textarea rows="8" placeholder="Status update here..." class="form-control" type="text" name="test" ng-model="post.text" ng-minlength=1 required></textarea>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="clear()">
                                    <span class="glyphicon glyphicon-ban-circle"></span> Cancel
                                </button>
                                <button type="submit" ng-disabled="form.$invalid" class="btn btn-primary">
                                    <span class="glyphicon glyphicon-save"></span> Save
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

Update function

$scope.update = function (id) {
        $scope.post = Post.get({id: id});
        $('#savePostModal').modal('show');
    };

I know it's successfully calling this get function, but the data from the $scope.post isn't being used.


Solution

  • As it turns out, in order for a modal to work, it has to be in the same controller-div as the text that prints the button. I'm pretty sure this is because the scope on controllers is different if the div is different. Also, don't have a second modal with the same name for testing because it will inevitably fail because of the functions confusion over which one to use.

    <div id="toBeUpdated" ng-controller="postController">
       <div class="modal fade" id="savePostModal" tabindex="-1" role="dialog" aria-labelledby="myPostLabel" aria-hidden="true">
          <div class="modal-dialog">
             <div class="modal-content">
                <form name="form" role="form" novalidate class="ng-scope ng-invalid ng-invalid-required ng-dirty ng-valid-minlength" ng-submit="createMob()">
                   <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal" aria-hidden="true" ng-click="clear()">&times;</button>
                      <h4 class="modal-title" id="myPostLabel">Create or edit a Post</h4>
                   </div>
                   <div class="modal-body">
                      <div class="form-group">
                         <label>ID</label>
                         <input type="text" class="form-control" name="id" ng-model="post.id" readonly>
                      </div>
                      <div class="form-group">
                         <label>Title</label>
                         <input type="text" class="form-control" name="test" placeholder="Title your post..." ng-model="post.name" ng-minlength=1 required>
                      </div>
                      <div class="form-group">
                         <label>Author</label>
                         <input type="text" class="form-control" name="test" placeholder="Temporary Field..." ng-model="post.author" ng-minlength=1 required>
                      </div>
                      <div class="form-group">
                         <label>Text</label>
                         <textarea rows="8" placeholder="Status update here..." class="form-control" type="text" name="test" ng-model="post.text" ng-minlength=1 required></textarea>
                      </div>
                   </div>
                   <div class="modal-footer">
                      <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="clear()">
                      <span class="glyphicon glyphicon-ban-circle"></span> Cancel
                      </button>
                      <button type="submit" ng-disabled="form.$invalid" class="btn btn-primary">
                      <span class="glyphicon glyphicon-save"></span> Save
                      </button>
                   </div>
                </form>
             </div>
          </div>
       </div>
       <li class="animate-repeat" ng-repeat="post in posts.slice().reverse() | checkboxAndFilter:search.$:search.author:search.name:search.id:search.text | limitTo: noPostsOnPage">
          <div class="well ng-scope">
             <div name="desktopPost" class="h4">
                <input type="checkbox" name="checkered"> &emsp;{{post.name}}&emsp;&emsp; <span ng-if="post.summary==1" class="animate-if"><button class="btn btn-xs disabled"><span class="glyphicon glyphicon-star-empty"></span></button></span>
                <a class="pull-right" ng-click="changeid(post.id);alignSearch();">{{post.id}}</a>
             </div>
             <div style="display: none" name="mobilePost" class="h4">
                <div ng-click="postNameAlert(post.name);">
                   <div name="ellipse" style="white-space: nowrap; overflow: hidden;text-overflow: ellipsis;-o-text-overflow: ellipsis;">{{post.name}}</div>
                </div>
                <input type="checkbox" name="checkered"> &emsp; 
                <span ng-if="post.summary==1" class="animate-if"><button class="btn btn-xs disabled"><span class="glyphicon glyphicon-star-empty"></span></button></span>
                &emsp;&emsp; 
                <a class="pull-right" style="padding-top:7px" ng-click="changeid(post.id);alignSearch();">{{post.id}}</a>
             </div>
             <p>
             <div ng-bind-html="trustHtml(post.text)" hashtagify term-click="tagTermClick($event)" user-click="tagUserClick($event)"></div>
             </p>
             <br> {{post.author}} on {{post.date_Written}}
             <br>
             <div name="desktopSide">
                <button class="btn btn-primary btn-xs" type="submit" ng-click="update(post.id)" class="btn">
                <span class="glyphicon glyphicon-pencil"></span> Edit
                </button>
                <button class="btn btn-danger btn-xs" type="submit" ng-click="delete(post.id);deleteSummary(post.id);" class="btn btn-danger">
                <span class="glyphicon glyphicon-remove-circle"></span> Delete
                </button>
             </div>
             <div style="display:none;" name="mobileSide">
                <button class="btn btn-primary btn-xs" type="submit" ng-click="updateMob(post.id)" class="btn">
                <span class="glyphicon glyphicon-pencil"></span> Edit
                </button>
                <button class="btn btn-danger btn-xs" type="submit" ng-click="delete(post.id);deleteSummary(post.id);" class="btn btn-danger">
                <span class="glyphicon glyphicon-remove-circle"></span> Delete
                </button>
             </div>
             <div class="animate-repeat" ng-repeat="summarized in filterID(post.id)">
                <div class="animate-repeat" ng-repeat="new_post in getPost(summarized.child)">
                   <button class="pull-right btn btn-default btn-xs" ng-click="reversePostState(new_post.id, new_post.name, new_post.author)">
                   <span id="{{new_post.author}}" class="glyphicon glyphicon-plus"></span>
                   </button>
                   <div class="well ng-scope">
                      <h4>
                         {{new_post.name}}
                         <div id="{{new_post.name}}" style="display:inline;"> - {{new_post.author}}</div>
                         &emsp;&emsp; <span ng-if="new_post.summary==1" class="animate-if"><button class="btn btn-xs disabled"><span class="glyphicon glyphicon-star-empty"></span></button></span>
                         <a class="pull-right" ng-click="changeElemOnClick('id_search');changeid(new_post.id);">{{new_post.id}}</a>
                      </h4>
                      <div id="{{new_post.id}}" style="display:none;">
                         {{new_post.text}}
                         <br> {{new_post.author}} 
                         on {{new_post.date_Written}}
                         <br>
                      </div>
                   </div>
                </div>
             </div>
          </div>
       </li>
       <center ng-if="checkForPosts()">
          <button class="btn btn-default" ng-click="incrementLimit()">
          <span class="glyphicon glyphicon-arrow-down"></span>Show More Posts
          </button>
       </center>
    </div>