在node.js中的$ http对话框中加载页面之前获取数据

时间:2016-10-04 18:31:19

标签: javascript angularjs node.js

我正在尝试开发一个动态对话框。文本来自$ http.get响应。但不知怎的,它不起作用。一点帮助将非常感激。我正在使用node.js和angular。

angular.module('rugCoPro')

.controller('DashboardController', [
    '$scope',
     'Session',
     '$location',
     'ObjectUtils',
     '$http',
     '$state',
     '$stateParams',
     '$mdDialog',
     function(
        $scope,
        session,
        $location,
        ObjectUtils,
        $http,
        $state,
        $stateParams,
        $mdDialog
    ){


    if(!session.isAuthenticated()){
        session.logout();
    }

    $http.get("api/models/department").success(function(response, status, headers){
        $scope.app_modes = response;
    });

    $scope.load = function(ev) {
        $mdDialog.show({
          controller: DialogController,
          template:
           '<md-dialog aria-label="List dialog">' +
           '  <md-dialog-content>'+
           '    <md-list>'+
           '      <md-list-item ng-repeat="item in items">'+
           '       <p>{{item.name}}</p>' +
           '      </md-list-item>'+
           '    </md-list>'+
           '  </md-dialog-content>' +
           '</md-dialog>',
          parent: angular.element(document.body),
          targetEvent: ev,
          clickOutsideToClose: true,
          locals: {
           items: $scope.app_modes
         },
        });
      };

    function DialogController($scope, $mdDialog) {
        $scope.items = $scope.app_modes;
        $scope.hide = function() {
          $mdDialog.hide();
        };

        $scope.cancel = function() {
          $mdDialog.cancel();
        };

        $scope.answer = function(answer) {
          $mdDialog.hide(answer);
        };
    }
}]);

模板ejs代码

<md-content ng-controller="DashboardController as ctrl" class="md-padding" ng-init='load()'>

    <div layout="row" layout-align="start center"> 
        <h6 class="md-headline">{{"Turco Persian Rug Co."| uppercase}}</h6>
    </div>

</md-content>

我正在尝试显示&#39; app_modes&#39;在md-dialog上列出。

1 个答案:

答案 0 :(得分:0)

解决您问题的最简单方法是:

$http.get("api/models/department").success(function(response, status, headers){
        $scope.app_modes = response;
        $scope.load();
    });

你放松了ng-init =&#34; load()&#34;。它将首先加载数据,然后显示对话框。