angular js showing fetched data to ngDialog

292 views Asked by At

I want to fetch data from server and show it to ngDialog dialog box

I an trying following code

var myApp = angular.module('myApp', ['ui.router', 'ngDialog']);
myApp.controller('test', function($scope, $http, ngDialog) {
  $scope.plist = [];
  $scope.pno = 1;
  $scope.likeDetails = function(item) {
    var data = 'id=' + item.id + "&userid=" + $scope.userid +
      "&pno=" + $scope.ppno;
    $http({
      method: 'POST',
      url: "show-liked-users",
      data: data,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }).then(function successCallback(rs, status, headers, config) {
      console.log(rs);

      for (var i = 0; i < rs.data.plist.length; i++) {
        $scope.plist.push(rs.data.plist[i]);
      }

    }, function errorCallback(data, status, header, config) {
      alert("Opps unable to connect to server");
    });
    ngDialog.open({
      template: 'liked-user-page',
      className: 'ngdialog-theme-plain',
      scope: $scope,
      data: $scope.plist
    });
  };
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.9/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.18/ui-router-angularjs.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ng-dialog/1.4.0/js/ngDialog.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ng-dialog/1.4.0/css/ngDialog.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ng-dialog/1.4.0/css/ngDialog-theme-default.css" />

<html>

<body ng-app="myApp">
  <div ng-controller="test">
    <div class="like-count" ng-click="likeDetails(s)">
      5
    </div>
  </div>
</body>

</html>

like-user-page url contains a page

<div ng-repeat="i in plist">
{{i.fname}} {{i.lname}}
</div>

here i calling `show-like-details' to get json data that is returning id,fname,lname of users. I want to show this data on ngDoalog box on open ngdialog it is shoing text liked-user-page

I want to iterare in plist insode 'like-user-page'(this is url representing page in server) data in and show it to ngdialog. and how to call url in ngDialog

1

There are 1 answers

0
Asif Aminur Rashid On

You should open your dialogue inside the success callback of the $http call.

var myApp = angular.module('myApp', ['ui.router', 'ngDialog']);
myApp.controller('test', function($scope, $http, ngDialog) {
  $scope.plist = [];
  $scope.pno = 1;
  $scope.likeDetails = function(item) {
    var data = 'id=' + item.id + "&userid=" + $scope.userid +
      "&pno=" + $scope.ppno;
    $http({
      method: 'POST',
      url: "show-liked-users",
      data: data,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }).then(function successCallback(rs, status, headers, config) {
      console.log(rs);

      for (var i = 0; i < rs.data.plist.length; i++) {
        $scope.plist.push(rs.data.plist[i]);
      }
      ngDialog.open({
        template: 'liked-user-page',
        className: 'ngdialog-theme-plain',
        scope: $scope,
        data: $scope.plist
      });
    }, function errorCallback(data, status, header, config) {
      alert("Opps unable to connect to server");
    });

  };
});