【问题标题】:Sharing data between Controllers using a Service, with Angular and Mean.js使用服务,使用 Angular 和 Mean.js 在控制器之间共享数据
【发布时间】:2015-10-15 15:08:51
【问题描述】:

好的,我几天前问过这个问题,还没有开始编写代码。现在在做了一些研究并玩弄了代码之后,我发现自己有点迷失了。

我会更好地解释自己并分享我到目前为止所做的代码。我仍然是 Angular 的“菜鸟”,所以我希望你们能帮助我做到这一点。

我正在使用 Mean.js 开发一个应用程序,该应用程序在客户端使用 Angular。 Mean.js 在不同的目录中分别创建模块,每个模块都有服务器和客户端目录。到目前为止,我创建了一个客户模块和一个约会模块(这些就像 CRUD 模块)

创建约会时,我希望能够从客户列表中选择一个客户(这意味着我必须从客户控制器获取该数据)

在创建约会视图中,我有这个 typeahead 组件,我想在输入客户姓名的同时显示客户:

<section data-ng-controller="AppointmentsController">
    <input type="text" ng-model="asyncSelected" typeahead="customer for customer in findCust()" typeahead-loading="loadingCustomers" class="form-control">
</section>

同时,在客户模块中,我使用 mean.js 的 yeoman 服务生成器创建了一个服务(这是我仍然不太了解的一件事),它基本上是一个模板,我只是添加了一些诸如对 Customers.query() 的调用(这是客户客户端控制器中的一个函数)、对服务器控制器的调用以获取更具体的客户列表:

'use strict';

angular.module('customers').factory('ShareService', [ 'Customers',
  function(Customers) {
    // Share service logic
    // ...

    // Public API
    return {
        listCustomers: function() {
            Customers.query(); //I added this call 
        }
    };
  }
]);

然后我将以下代码行添加到 Appointments 控制器中,希望开始让这个东西工作:

'use strict';

// Appointments controller
angular.module('appointments').controller('AppointmentsController',['$scope', '$stateParams', '$location', 
 'Authentication', 'Appointments','ShareService',
    function($scope, $stateParams, $location, Authentication, Appointments, ShareService) {
        $scope.authentication = Authentication;

        /* 
            Appointments CRUD ops logic here
        */

        // Find a list of Customers
        $scope.findCust = function() {
            ShareService.listCustomers();
        };
    }
]);

之后我去了应用程序,但没有发送数据,控制台在输入文本框时给我这个错误:

Cannot read property 'length' of undefined

所以当然没有达到我的预期。我知道我在这里可能有一些重大错误。事实证明,Angular 在某些方面非常简单,但在其他方面则很复杂(至少对我而言),模块/控制器之间的这种通信让我陷入了困境。

澄清一下,我在两个单独的目录(客户和约会)中有两个模块,我将 ShareService 添加到客户模块(因此我能够“共享”一些信息)。到目前为止,我得到的唯一错误是浏览器控制台给出的错误。 grunt 控制台中没有错误。但这有点混乱,所以我知道我做错了什么。什么?这就是问题所在。

感谢您的宝贵时间。

【问题讨论】:

    标签: angularjs mean meanjs


    【解决方案1】:

    好吧,您正在调用 listCustomers 中的 ShareService,但此服务没有使用此名称的方法。

    刚刚将您的ShareService返回的对象更改为:

    return {
        listCustomers: function() {
            Customers.query(); //I added this call
        }
    };
    

    编辑

    listCutomers 函数没有返回任何内容,这就是对象未定义的原因。只需像这样添加返回:

    return {
        listCustomers: function() {
            return Customers.query(); //I added this call
        }
    };
    

    【讨论】:

    • 谢谢你的回答,其实我忘记把ShareService中的someMethod名字改成listCustomers了,还是没有结果。但我确实打印了结果,它实际上是返回用户数组,所以问题似乎是在搜索框中显示它们时?除此之外,我仍然收到错误:无法读取未定义的属性“长度”
    • 这是我能够从 Customers.query( ) 登录到控制台的内容,但它似乎没有返回它,或者至少没有以正确的方式返回,因此它可以显示,如您所见它从用户列表中查询
    • [$promise: Object, $resolved: false]0: Resource__v: 0_id: "55a9288c43cc16746f470282"b_day: 9b_month: 1cod_depelle: "22222B"created: "2015-07-17T16:08:44.881Z “电子邮件:“andcorvil@gmail.com”名字:“André”last_pur:“2015-07-17T16:08:44.881Z”last_visit:“2015-07-17T16:08:44.881Z”lastname_1:“Cordero”lastname_2: "Villalobos"phone_1: "22222222"phone_2: ""referred: truesurname: ""user: Object
    • 查看我的编辑,因为我向 listCustomers 函数添加了返回语句。
    • 谢谢,补充说,它实际上到达了约会控制器,我做到了:console.log(ShareService.listCustomers());在 findCust 函数中,我得到了结果,但它仍然说它是未定义的,所以它无法得到它的长度。我做了 console.log(typeof ShareService.listCustomers());我得到了对象,所以看起来一切正常,但它不会通过搜索框
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2017-11-06
    相关资源
    最近更新 更多