AngularJS-如何从其他应用程序加载服务

AngularJS-如何从其他应用程序加载服务,angularjs,angularjs-directive,angularjs-service,Angularjs,Angularjs Directive,Angularjs Service,我有一个角度代码: 这是我的控制器: MyApp.controller('vtCustomerController', function ($scope, messageBus, customerDetails, $http, $q) { $scope.loadCustomerDetail = function (customerID) { customerDetails.loadCustomerDetails(customerID); }; }); M

我有一个角度代码:

这是我的控制器

MyApp.controller('vtCustomerController', function ($scope, messageBus, customerDetails, $http, $q) {


   $scope.loadCustomerDetail = function (customerID) {
        customerDetails.loadCustomerDetails(customerID);

    };


});
MyApp.factory('customerDetails', function($rootScope,$http, $q) {

        var customer = {};

        getCustomer = function()
        {
            return customer;
        };

       loadCustomerDetails = function(customerID) {
             $http.post('/customer/data/getsingleexpanded',
                   {'customer':customerID})
                    .success(function(data,status, headers, config){
                        angular.copy(data, customer);
                    })
                    .error(function(data,status, headers, config){
                        console.log(status);
                    });   
        };
});
这是我的服务

MyApp.controller('vtCustomerController', function ($scope, messageBus, customerDetails, $http, $q) {


   $scope.loadCustomerDetail = function (customerID) {
        customerDetails.loadCustomerDetails(customerID);

    };


});
MyApp.factory('customerDetails', function($rootScope,$http, $q) {

        var customer = {};

        getCustomer = function()
        {
            return customer;
        };

       loadCustomerDetails = function(customerID) {
             $http.post('/customer/data/getsingleexpanded',
                   {'customer':customerID})
                    .success(function(data,status, headers, config){
                        angular.copy(data, customer);
                    })
                    .error(function(data,status, headers, config){
                        console.log(status);
                    });   
        };
});
这是一种自动完成的方法:

angular.module('text-autocomplete',[])
                .directive('text-autocomplete', function(){
                    return {
                                require: '^ngModel',
                                restrict: 'A',
                                scope:{
                                    ngModel:'=',
                                    datasource:'=',
                                    minlength:'=',
                                    itemid:'='
                                },
                                link: function(scope, elem, attr, ctrl) {

                                        elem.autocomplete({
                                            autoFocus : !_.isUndefined(attr.autofocus)? true : false,
                                            open: function(event,ui){console.log('autocomplete open message received')},
                                            source: function(request,response){
                                                    scope.datasource(ctrl.$viewValue)
                                                         .then(function(result){
                                                             console.log('data source then function called');
                                                             console.log(result.data);
                                                                response(result.data)});
                                                },
                                            minLength: scope.minlength,
                                            select: function(event,ui){
                                                console.log('autofocus select called');
                                                console.log(ui);
                                                    //-1 indicates item not found
                                                 //store the information in the control. 
                                                 if(ui.item.value != '-1')
                                                 {
                                                         scope.ngModel = ui.item.label;
                                                         if(attr.itemid)
                                                         {
                                                             scope.itemid = ui.item.value;
                                                         }

                                                 }
                                                 else if(ctrl.$viewValue != '')
                                                 {
                                                     scope.ngModel = ctrl.$viewValue;
                                                     scope.itemid = 0;
                                                 }
                                                 else
                                                 {
                                                     console.log('setting the account number to blank');
                                                     scope.ngModel = '';
                                                     scope.itemid = -1;                                                     
                                                 }
                                                 scope.$apply();

                                                 return false;
                                                },
                                             focus: function(event,ui){
                                                return false;
                                                },
                                             close: function(event,ui){
                                                 console.log('autocomplete close called');
                                                 console.log(ui);
                                                }
                                          });



                                }
                    };
                });
我需要做的是,在自动完成的中选择,这是当您单击一个项目时)调用customerDetails.loadCustomerDetails方法(从服务)。但它在其他应用程序中,对吗


有没有办法做到这一点?或者,也许是更好的方法?

编辑-这只解决了OP对问题的误解,而@TongShen似乎为实际问题提供了解决方案

我认为你把应用程序的概念和模块的概念混为一谈了

应用程序往往需要一些额外的层(例如API)来相互通信,因此如果您演示的是应用程序,那么是的,您可能会遇到问题

相反,您的代码演示了模块的使用,模块可以与Angular一起广泛使用,以帮助分割和封装代码。如果每个模块都通过依赖注入进行了正确的接口,那么它们可以相互访问

下面是一个简单的演示,大致以您的代码为模式:

var myApp = angular.module('myApp', ['otherApp']);
var anotherModule = angular.module('otherApp', []);

myApp.factory('MyFactory', function(){
  var stuff = {};

  stuff.doSomething = function(){
    console.log('just did something');
  }

  return stuff;
});

anotherModule.directive('myDirective', function(MyFactory){
  return {
    restrict: 'E',
    link: function(){
      MyFactory.doSomething();
    }  
  }
});

。。。您可以看到,
console.log
确实启动了。

您可以将
loadCustomerDetails
函数(从控制器范围)传递到指令中

在指令的范围内定义如下:

scope:{
    ngModel:'=',
    datasource:'=',
    minlength:'=',
    itemid:'=',
    autoComplete: '&'
},
并更新HTML中的指令用法以添加:

<...... text-autocomplete ...... auto-complete="loadCustomerDetails($customerId)">
这应该行得通

附言


你可以说一个应用是一个模块,但并非所有模块都是应用。有时,您可以创建只包含服务、资源或其他内容的模块。只要正确指定依赖项,不同模块的服务或功能可以在一个应用程序中很好地结合使用。

请注意,标记不是关键字。在标签列表中填入与问题相同的单词(应用程序、指令、服务),无助于对其进行分类。标签也是独立的。也就是说,您不能组合多个标记来创建单个概念。例如,标记
[angularjs]
[directive]
与单个
[angularjs directive]
标记不是一回事。务必阅读选择标签时出现的说明!经过3个小时的调查(我自己)我没有解决这个问题。有了你的回答,我只用了10分钟。非常感谢你!!