【问题标题】:Adding http module to ionic framework将http模块添加到离子框架
【发布时间】:2015-08-24 14:12:29
【问题描述】:

我正在尝试从我的 ionic 应用程序进行 http 调用以返回一些 json 数据。但它显示了错误:

[$injector:nomod] 模块“$http”不可用!

我想我必须将模块 http 添加到 Angularjs。请让我知道如何将 http 等新模块添加到 ionic 框架。谢谢。

【问题讨论】:

  • 请出示您的代码?
  • $http.post('/someUrl', {msg:'hello word!'})。 then(function(response) { // 此回调将被异步调用 // 当响应可用时 }, function(response) { // 如果发生错误则异步调用 // 或服务器返回带有错误状态的响应。}) ;
  • angular.module('starter.controllers', []) // 一个从服务中获取数据列表的简单控制器 .controller('PetIndexCtrl', function($scope, PetService) { // "Pets" 是一个返回模拟数据的服务 (services.js) $scope.pets = PetService.all(); }) // 一个简单的控制器,显示一个被点击的项目的数据 .controller('PetDetailCtrl', function($ scope, $stateParams, PetService) { // "Pets" 是一个返回模拟数据的服务 (services.js) $scope.pet = PetService.get($stateParams.petId); });
  • 是的。 @evc 是对的。看看他的回答。

标签: angularjs ionic-framework ionic


【解决方案1】:

$http 不是模块,是服务,像这样注入的

angular.module('app').controller('TestController', function($scope, $http){
    //use $http here
    $http.get('https://stackoverflow.com').then(function(successResponse){
       $scope.data = successResponse;
    }, function(errorRepsonse){
       $scope.error = errorRepsonse;
    });
});

Docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2013-12-26
    • 2018-06-16
    • 2018-04-24
    • 1970-01-01
    • 2015-05-02
    相关资源
    最近更新 更多