$ http.get在.factory上不工作 [英] $http.get not working at .factory

查看:79
本文介绍了$ http.get在.factory上不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ionic构建应用程序.首先,我将基于TABS模板创建一个项目. 因此,创建后,我试图通过API进行聊天,但不起作用.那就是services.js的代码:

I'm building a App with Ionic. To start, I create a project based on TABS template. So, after the creation, i'm trying to get my chats by an API but don't work. That's the code of services.js:

angular.module('starter.services', [])

.factory('Chats', function($http) {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  // var chats = [{
  //   id: 0,
  //   name: 'Ben Sparrow',
  //   lastText: 'You on your way?',
  //   face: 'img/ben.png'
  // }, {
  //   id: 1,
  //   name: 'Max Lynx',
  //   lastText: 'Hey, it\'s me',
  //   face: 'img/max.png'
  // }, {
  //   id: 2,
  //   name: 'Adam Bradleyson',
  //   lastText: 'I should buy a boat',
  //   face: 'img/adam.jpg'
  // }, {
  //   id: 3,
  //   name: 'Perry Governor',
  //   lastText: 'Look at my mukluks!',
  //   face: 'img/perry.png'
  // }, {
  //   id: 4,
  //   name: 'Mike Harrington',
  //   lastText: 'This is wicked good ice cream.',
  //   face: 'img/mike.png'
  // }];
  var chats = function(){ 
    $http.get("http://lucassmuller.com/work/projetoblog/api.php?action=posts")
    .success(function(data) {
        return data;
        console.log='data success';
    })
    .error(function(data) {
        console.log='data error';
    });
  }

  return {
    all: function() {
      return chats;
    },
    remove: function(chat) {
      chats.splice(chats.indexOf(chat), 1);
    },
    get: function(chatId) {
      for (var i = 0; i < chats.length; i++) {
        if (chats[i].id === parseInt(chatId)) {
          return chats[i];
        }
      }
      return null;
    }
  };
});

但是这不起作用.我该怎么做才能使$ http获得API的数据!?

But this aren't working. That can I do to make the $http get the data of API!?

推荐答案

由于已经回答了该问题,请允许我指出您的代码存在的问题,没人关心.

Since the question is already answered, please allow me to point out an issue with your code that nobody cared to talk about.

您正在代码中使用它们:

You're using these in your code:

//..
console.log='data success';
// some code     
console.log='data error';

使用console.log的正确方法是将参数传递给它,因为它是一个函数.很遗憾,您的浏览器具有以下代码:

The correct way to use console.log is to pass a parameter to it since its a function. Rougly, your browser has code like this:

function console() {
  this.log = function() {
    // code that logs to the console
  }
}

执行console.log = 'data success'时,将字符串'data success'分配给控制台objectlog属性.缺少对实际功能代码的引用,并且console.log将无法正常使用该页面的其余部分.

When you do console.log = 'data success', you are assigning the string 'data success' to the log property of console object. There reference to the actual functional code is lost and console.log will not work the way it should for rest of the page.

console.log('I work fine');
console.log = 'I screw things up!';
console.log('I cannot log to the console'); // every `console.log`s below here do not work

这篇关于$ http.get在.factory上不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆