【发布时间】: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 控制台中没有错误。但这有点混乱,所以我知道我做错了什么。什么?这就是问题所在。
感谢您的宝贵时间。
【问题讨论】: