How to load data into ngDialog

I have a requirement where I need to open a dialog from a jsp page and when the dialog opens I need to load it with some pre-filled data from the server (using an AJAX call). If I make an AJAX call before opening the dialog, I get the data, but the dialog is loaded as a new page. If I try to get data in a new controller, the dialog still doesn't reflect the data. What should I use to make sure the dialog reflects the data I am receiving from the server.

<div class="container-fluid" ng-controller="EditUserController">

    <div class="text-center container-fluid">
        <label class="sub-header">Edit User: {{userEmail}}</label>
    </div>

    <form action="editUser" method="post" name="editForm">
        <div>
            <div class="pull-right">
                <label>Delete User</label><br> <a href="#"
                    class="btn btn-block btn-sm btn-danger" ng-click="deleteUser(userEmail)">{{userEmail}}</a>
            </div>
            <div>
                <label>Change Role</label>
            </div>
            <div>
                <label>
                <input type="checkbox" ng-model="superVisor" name="superVisorFlag" 
                ng-true-value="1" ng-false-value="0" value="${existingUser.superVisorFlag}">
                Make a Supervisor</label>
            </div>
            <div>
                <input type="text" class="form-control" ng-model="email"
                    name="emailAddress" ng-disabled = "true"
                    ng-options="email for email in userEmail"
                    value="${existingUser.emailAddress}"
                    placeholder="Enter New User Email Address" bs-typeahead>
            </div>
            <div>
                <input type="text" class="form-control" ng-model="firstName"
                    name="firstName" value="${existingUser.firstName}"
                    placeholder="Enter First Name" bs-typeahead>
            </div>
            <div>
                <input type="text" class="form-control" ng-model="lastName"
                    name="lastName" value="${existingUser.lastName}"
                    placeholder="Enter Last Name" bs-typeahead>
            </div>


            <div>
                <a href="#" class="btn btn-sm btn-primary" ng-click="saveChanges()">Save Changes</a>
            </div>
        </div>
    </form>

</div>

<script type="text/javascript"
    src="<c:url value="/resources/scripts/admin.js"/>"></script>

      

The above jsp is for dialog. Below is my js file -

var app = angular.module('scc-admin', [ 'ngDialog', 'mgcrea.ngStrap' ]);
app.factory("UserList", function() {
    var UserList = {};
    UserList.data = [ {
        userId : 111,
        userFirstName : "xxx",
        userLastName : "yyy",
        userEmail : "xxx.yyy@zzz.com",
        userRole : "Admin"
    }, {
        userId : 222,
        userFirstName : "second",
        userLastName : "last",
        userEmail : "second.last@zzz.com",
        userRole : "Manager"
    }];
    return UserList;
});
app.controller('UserSettingsController', function($scope, ngDialog, UserList,$http) {
    // variable for the bashboard list
    $scope.userList = UserList;
    $scope.editUser = function(userEmail) {             
        $scope.userEmail = userEmail;       
        ngDialog.open({
            template : 'editUser' ,
            className : 'ngdialog-theme-default',
            controller : 'EditUserController',
            closeByEscape : true,
            scope : $scope
        });
    };

    $scope.addUser = function() {
        ngDialog.open({
            template : 'addUser',
            className : 'ngdialog-theme-default',
            controller : 'AddUserController',
            closeByEscape : true,
            scope : $scope
        });
    };

});

app.controller('EditUserController', function($scope, ngDialog, $http) {

    ngDialog.template = $scope.output;
    ngDialog.$modelValue = $scope.output;

    var responsePromise = $http.get("initUser?email=" + $scope.userEmail);
        responsePromise.success(function(data, status, headers, config) {
                $scope.output = data;
                console.log(data);                
        });
    console.log($scope);

    $scope.deleteUser = function(){

        $scope.cfdump = "";

        var str = {emailAddress : $scope.userForm.emailAddress.$modelValue};
        str = JSON.stringify(str);

        var request = $http({
            method: 'post',
            url: "deleteUser?formData=" + str,
            data: ({formData:str})
        });

        request.success(function(html){
            alert("success");
        });

        request.error(function(errmsg){
            alert("Unable to delete user");     
        });     

    }

});

      

I open a dialog in the usersettings controller and try to load it with default data. I tried to set up a new dialog template to output an AJAX call, it didn't work. What am I missing here?

+3


source to share


2 answers


After consulting the documentation, I found out the following solution. It should work for you as it does for me.

To pass data (JSON object) for ng model internally ngDialog

, you can declare yours ngDialog

like this.

ngDialog.open({
    template: 'my-template.html',
    className: 'ngdialog-theme-plain',
    data: $scope.myJSONObject
});

      

Now that you've completed the part above, you need to bind the data in your ngDialog popup, so go ahead and put ngDialogData.myJSONObjectFieldName

in yours ng-model

.



Consider the following example for further development. Suppose we have myJSONObject

as follows.

myJSONObject={
   first_name: 'John',
   last_name: 'Doe'
};

      

To use first_name

inside your ngDialog ng-model

just put ng-model="ngDialogData.first_name"

.

+19


source


To check if controller (VM) data is received in Modal Dialog,

 <pre>{{vm|json}}</pre>

      

Module and controller:

var app = angular.module("DemoApp",['ngDialog']);
    app.controller("DemoController",["$rootScope","ngDialog","productService",function($rootScope,ngDialog,productService){
        var vm=this;
         $rootScope.ngDialog = ngDialog; // to close Dialog using "ngDialog.close();" in ProductDialog.html

 /* vm.products=[{brand:"Apple", price:60000, os:"iOS"},
             {brand:"Samsung", price:35000, os:"Android"},
             {brand:"Microsoft Lumia", price:30000, os:"Windows 10"}
            ];  
 */

vm.getProductDetails=function() {
            productService.getData().then(function (response) {
                if (response.data) {
                    vm.products=response.data;
                    vm.ProdDialog();
                }
            });
    };

vm.productPopup = function(x){
    ngDialog.open({
        template: 'ProductDialog.html',
        className:'ProductDetailsDialog'
        scope:$scope,
        data:x,
        closeByDocument:true
    });
}
      vm.getProductDetails();
}]);

      

Services:



app.factory("productService",["$http",function($http){
    return {
        getData: function() {
            return $http.get("http://xxxxxxx/xxx/xxxxx");
        }
     };
}]);

      

DemoController.html

   /* <table>
        <tr>
            <th>Brand</th>
            <th>Price</th>
            <th>OPerating System</th>
            <th>Open ngDialog</th>
        </tr>
        <tr  ng-repeat="x in vm.products">
            <td ng-bind="x.brand"></td>
            <td ng-bind="x.price| currency:"&#8377;":2"></td>
            <td ng-bind="x.os"></td>
            <td><button ng-click="vm.productPopup(x)"></button></td>

        </tr>
    </table>
*/

      

ProductDialog.html:

<div class="ProductDetailsDialog">
    <div class="ngdialog-content" role="document">
        <div class="modal-header">
            <button type="button" class="close" ng-click="ngDialog.close();">&times;</button>
            <h4 class="modal-title">Product Detials</h4>
        </div>
               //  <pre>{{vm|json}}</pre> //
        <div class="modal-body">
            <h4>Brand:<span ng-bind="ngDialogData.brand"></span></h4>
            <h4>Price:<span ng-bind="ngDialogData.price | currency:"&#8377;":2"></span></h4>
            <p>Operating System:<span ng-bind="ngDialogData.os"></span></p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default">OK</button>
        </div>
    </div>
</div>

      

0


source







All Articles