角度JS控制器功能没有进入

时间:2015-12-11 15:36:32

标签: javascript angularjs

我试图简单地将一些JSON数据从GET调用中抛出到页面上。

这是我的HTML(请注意,它已加载到index.html中,其中包含正确的角度表示法)

<h1>Downloads</h1>
<div class="container" ng-controller="DownloadCtrl as download">
  <p>{{download.routes}}</p>
</div>

这是下载控制器:

(function(){
  'use strict';

  angular.module('dashboardApp').controller('DownloadCtrl', DownloadCtrl);
  DownloadCtrl.$inject= ['DownloadService'];

  function DownloadCtrl(DownloadService){
    var self = this;
    self.routes = function(){
      DownloadService.getRoutes()
        .then(function(responseData) {
          self.routes = responseData;           
      });
    };
  };
})();

这是下载服务:

(function(){
  'use strict';

  angular.module('dashboardApp')
    .factory('DownloadService', DownloadService);

  DownloadService.$inject = ['$http', '$sessionStorage'];

  var baseURL = 'http://localhost:8080/Dashboard/rest/download/';

  function DownloadService ($http, $sessionStorage){
    var service = {};
    service.getRoutes = getRoutes;
    return service;

    function getRoutes(){
      return $http.get(baseURL+"route",$sessionStorage.sessionData.sessionID);
    }
  }
})();

我已经调试了应用程序,它确实点击了 self.routes 但它只是跳过它并且没有显示任何数据。

我也没有在控制台中收到任何错误。它只是跳过了这个功能。

这是我的第一个AngularJS应用程序。

1 个答案:

答案 0 :(得分:1)

你的代码组织不好, 错误位于视图中,因为它没有调用方法self.routes,只是打印出来...

你的观点必须做那样的事情:

<p>{{download.routes()}}</p>

但是,这是一种不好的编码方式...... 请考虑做类似的事情:

&#13;
&#13;
DownloadService
  .getRoutes()
  .then(function(responseData){
    self.routes = responseData;         
  })
;

// instead of

self.routes = function(){

  DownloadService.getRoutes()
  .then(function(responseData){
    self.routes = responseData;         
  });

};
&#13;
&#13;
&#13;