【问题标题】:Unable to get property 'get' of undefined or null reference无法获取未定义或空引用的属性“获取”
【发布时间】:2016-05-22 23:03:18
【问题描述】:

我知道这可能很简单,但我收到了这个错误

无法获取未定义或空引用的属性“get”

当我从控制器调用我的 api 时

 rmdsController.$inject = ['$scope', 'rmds'];
function rmdsController($scope, rmds, $http) {

$scope.Calculate = function () {
        alert('made it');
        $("#spinner").show();


          $http.get('/api/rmd/calcRMDdist/')

          .success(function (data) {
              // Do stuff with data.
          })
          .catch(function (err) {
              // Log error somehow.
          })
          .finally(function () {
              // Hide loading spinner whether our call succeeded or failed.
              $scope.loading = false;
          });
    }

【问题讨论】:

  • 您尚未将$http 注入控制器。 $inect 语句中缺少它。另外,不要使用.success,它已被弃用;请改用.then

标签: angularjs asp.net-mvc angular-controller


【解决方案1】:

依赖注入是一种常用于基础架构组件的模式,它确保一个特定组件不会直接创建对其他组件的引用。每个组件都不会直接实例化,而是接收对所需其他组件(如助手、服务等)的引用作为其构造函数的参数。在你的情况下是这样的:

rmdsController.$inject = ['$scope', 'rmds', '$http'];

这里,每当这个控制器被实例化时,Angular 都会注入 $scope、rmds、$http。

参考: http://henriquat.re/basics-of-angular/services-dependency-injection/services-and-dependency-injection-in-angularjs.html

【讨论】:

    猜你喜欢
    • 2014-08-01
    • 2020-04-28
    • 2016-08-16
    • 2017-05-18
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多