AngularJS从外部模块引用factory.function

时间:2013-10-22 15:46:05

标签: javascript angularjs

我为下面定义的angularjs提供了两个不同的应用程序文件:

search.js

var app = angular.module('search',['ngGrid']);
    app.factory('answers',function($http){  
        return{  
           search: function(params,success,error){  
            //code to execute over rest    
         },  
            otherSearch: function(params,success,error){  
            //code to execute over rest    
         }
        };  
});

results.js

var app = angular.module('results',['ui.bootstrap','ngGrid']);  
    app.controller('resultsCtrl',function($scope,$http,search) // search should reference search.js  
    search.search();  
    search.otherSearch();

});

我要做的是引用search otherSearch中的search.jsresults.js函数但是,当我这样做时,未知提供程序失败了:< / p>

var app = angular.module('results',['ui.bootstrap','ngGrid','search']);  

制作它的正确方法是什么,以便我可以调用FACTORY

中包含的search.js方法

1 个答案:

答案 0 :(得分:0)

在以下代码中,您所做的就是重新声明名为search的Angular应用:

var app = angular.module('search',['ngGrid']);

在下面的代码中,您实际上是在声明工厂,并且已将其命名为answers

    app.factory('answers',function($http){  
        return{  
           search: function(params,success,error){  
            //code to execute over rest    
         },  
            otherSearch: function(params,success,error){  
            //code to execute over rest    
         }
        };  
});

我认为问题是你正试图将answers注入search。试试这个:

app.controller('resultsCtrl',function($scope,$http,answers)
    answers.search();  
    answers.otherSearch();