当使用外部数据不起作用时链接到下一页 - Ionic - AngularJS

时间:2016-02-15 15:40:27

标签: javascript angularjs json ionic-framework

我是离子和棱镜的新手,我不知道我哪里出错了。我试图从外部链接获取json。它返回数据并完美地显示它们,但当我点击返回列表中的一个选项时,它不会重定向到通知详细信息页面。如果json数据被编码,一切正常,但我不想这样做。这是处理列表和链接的控制器和服务:

.service('NoticeboardService', function($q, $http) {
   mynotices = $http.get('http://.../app/noticeboard.php').then(function(response){    
            console.log(response.data)          
            return response.data
          })
   return {
      notices: mynotices, // if i change this so that it is hard-coded in the links work
      getNotices: function() {
      return this.notices
   },
   getNotice: function(noticeId) {
      var dfd = $q.defer()
      this.notices.forEach(function(notice) {
         if (notice.id === noticeId) dfd.resolve(notice)
      })

      return dfd.promise
   }

}
})

.controller('NoticesCtrl', function($scope, notices) {
    $scope.notices = notices
})

.controller('NoticeCtrl', function($scope, notice) {
    $scope.notice = notice
})

我正在显示的数据中的示例:

[
  {

  "id": "1",
  "title": "Notice one",
  "content": null,
  "image": "uploads/news_images/2d3a24b738430c94f3.png",
  "date": "November 20, 2015, 3:19 pm"

  },
  {

  "id": "2",
  "title": "Notice Item",
  "content": null,
  "image": "uploads/news_images/6ff8308fd9f2a7baf3.png",
  "date": "November 20, 2015, 3:21 pm"

  }
]

当我使用console.log('notices:', mynotices);时 返回以下内容:notices: Object { $$state: Object } 为什么它不返回对象数组?

1 个答案:

答案 0 :(得分:0)

我通过将服务更改为以下内容来解决问题。为此,我使用此link来帮助我解决它。我不完全确定为什么我的原始代码不起作用,但现在这样做:

.service('NoticeboardService', function($q, $http) { 

  var notices = [];

  return {
    getNotices: function(){
      return $http.get("http://fitnexus.com/app/noticeboard.php").then(function(response){
         notices = response.data;
         return notices;
      });
    },
    getNotice: function(noticeId){
      for(i=0;i<notices.length;i++){
         if(notices[i].id == noticeId){
            return notices[i];
         }
      }
      return null;
   }
 }
})