【问题标题】:I inject package into angular module, why is my package null?我将包注入角度模块,为什么我的包为空?
【发布时间】:2014-09-03 09:28:04
【问题描述】:

我正在使用猫鼬将对象存储到我的数据库中。我已经成功地将一个对象保存到数据库中,现在每当我尝试搜索它时,我都会在“Compositions.query(function ...”上得到一个未定义的结果。我有这个模块,称为 composition,这是它的控制器:

angular.module('mean.compositions').controller('CompositionsController', ['$scope','fileUpload' ,'$stateParams', '$location', 'Global', 'Compositions',
  function($scope, fileUpload, $stateParams, $location, Global, Compositions) {
    $scope.global = Global;
    $scope.package = {
      name: 'compositions'
    };
$scope.find = function() {
      console.log('get here');
      Compositions.query(function(composition) {
        console.log(composition);
        $scope.composition = composition;
      });
      console.log('pass');
    };
}

现在,每当我尝试查询合成时,都会收到此错误:

undefined is not a function
    at Scope.$scope.find (http://localhost:3000/modules/aggregated.js:6:644)
    at http://localhost:3000/bower_components/angular/angular.js:10735:21
    at Scope.$eval (http://localhost:3000/bower_components/angular/angular.js:12595:28)
    at pre (http://localhost:3000/bower_components/angular/angular.js:19781:15)
    at nodeLinkFn (http://localhost:3000/bower_components/angular/angular.js:6628:13)
    at compositeLinkFn (http://localhost:3000/bower_components/angular/angular.js:6039:13)
    at publicLinkFn (http://localhost:3000/bower_components/angular/angular.js:5934:30)
    at http://localhost:3000/bower_components/angular-ui-router/release/angular-ui-router.js:2805:9
    at nodeLinkFn (http://localhost:3000/bower_components/angular/angular.js:6648:13)
    at compositeLinkFn (http://localhost:3000/bower_components/angular/angular.js:6039:13) <section data-ng-controller="CompositionsController" data-ng-init="find()" class="ng-scope">

我已包含服务代码。

更新:

 'use strict';

angular.module('mean.compositions').factory('Compositions', [
    function() {
        return {
            name: 'compositions'
        };
    }
]);

感谢任何形式的输入,为什么我会收到这个奇怪的错误。

【问题讨论】:

  • 您可以发布Compositions 服务吗?
  • @AnthonyChu 我已经用服务代码更新了我的帖子。
  • 对不起,我的意思是 Compositions 服务被注入到控制器中。那是什么以及它是如何定义的?
  • @AnthonyChu 我是 Angular 新手,但我认为新的更新应该是您所要求的?
  • 是的,就是这个。 Compositions 只是一个具有 name 属性的对象。这就解释了为什么当您尝试调用其不存在的 query() 方法时会出现该错误。您的意思是从工厂返回资源吗?

标签: node.js angularjs mongoose mean-stack


【解决方案1】:

Composition 是您使用以下代码创建的服务:

'use strict';

angular.module('mean.compositions').factory('Compositions', [
    function() {
        return {
            name: 'compositions'
        };
    }
]);

它唯一拥有的是一个名为name 的属性,其值为compositions。您要做的是将Composition 服务注入CompositionsController 并在其上调用query 方法。记住这一点,为什么整个事情会因undefined is not a function 错误而失败并不令人费解。

可能需要做的是向Compositions 工厂函数返回的对象添加一个query 方法,并从那里返回一些有意义的东西。

return {
    name: 'compositions',
    query: function() { 
        console.log("This is where I have to access my DB'); 
        return ['object1', 'object2', 'object3'] }
    };

【讨论】:

  • 这行得通,除了我使用了我定义的 $resource 依赖项。谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 2016-10-18
  • 2019-02-25
  • 2023-03-18
  • 2017-12-27
  • 2018-04-25
  • 1970-01-01
相关资源
最近更新 更多