没有定义变量$ HTTP变量、定义、HTTP

2023-09-13 04:46:38 作者:柒夏

我想建立一个应用程序Angularjs,我有我的控制器的麻烦。

I'm trying to build an Angularjs application and I'm having trouble with my controller.

'use strict';

/* Controllers */

angular.module('myApp.controllers', []).
  controller('AppCtrl', function ($scope, $http) {

  }).
  controller('indexCTRL', function ($scope) {
    $http.get('/api/frettir').
    success(function(data, status, headers, config) {
      $scope.posts = data;
    });

  });

但是当我运行一个错误出现说$ HTTP没有定义。我可以改进?

But when I run it an error appears saying $http is not defined. What can I improve?

推荐答案

您需要使用它的控制器注入 $ HTTP 服务。我喜欢这个非常precise 内嵌语法,以避免出现问题,如果你的code去通过minifiers。

You need to inject the $http service in the controllers you use it. I like this very precise inline syntax which will avoid problems if your code goes through minifiers.

controller('indexCTRL', ['$scope', '$http',
    function($scope, $http) {
     //...your code

    }
])