【问题标题】:Cannot read property 'get' of undefined with Angular无法使用 Angular 读取未定义的属性“get”
【发布时间】:2014-08-20 03:01:42
【问题描述】:

我正在使用 Cordova 和 Angular JS 开发一个移动应用程序。我的应用程序位于 easyphp 本地主机上。 我在 index.html 中使用带有 ng view 指令的 Angular Routes。我有这个:

TypeError:无法读取未定义的属性“get”

angular.module('app.controllers', [])
.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.status = "Accueil";
}])
.controller('ViewCtrl', ['$scope', function ($scope, $http) {
    $http.get('mobile.php/getAnnonces/limit=10')
        .success(function(data, status, headers, config) {
          $scope.posts = data;
        })
        .error(function(data, status, headers, config) {
          // log error
    });
}])
...

如果我测试 URL,我的脚本会返回 JSON(带有 json_encode)。我哪里错了?

感谢您的帮助,

问候

【问题讨论】:

  • 在 ViewCtrl 中,您忘记将依赖 $http 作为字符串传递,然后再将其作为参数传递

标签: angularjs


【解决方案1】:

尝试改变

angular.module('app.controllers', [])
.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.status = "Accueil";
}])
.controller('ViewCtrl', ['$scope','$http', function ($scope, $http) {
    $http.get('mobile.php/getAnnonces/limit=10')
        .success(function(data, status, headers, config) {
          $scope.posts = data;
        })
        .error(function(data, status, headers, config) {
          // log error
    });
}]);

变化: 将$http 服务作为字符串传递给控制器​​,以便在运行时解析。

【讨论】:

  • 指出您更改的内容和原因(即添加'$http')会很有帮助。
  • 您应该使您的更改更加明确。这里缺少的是 ViewCtrl 的依赖项数组中的“$http”
  • 再次感谢您的快速回答,它完美运行;)
  • 如果我添加相同的内容,我仍然会收到相同的错误“无法读取未定义的属性 'get'”。需要您的帮助。
猜你喜欢
  • 2020-04-27
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 2020-09-04
  • 2021-04-29
  • 2021-12-01
相关资源
最近更新 更多