我正在使用刀片模板引擎。我想遵循“每页一个控制器”的约定来轻松处理代码。我有一个JS文件,其中几乎没有控制器将在所有文件/页面中使用,所以我包含在模板文件中,该模板文件将在每个页面中自动生成。代码如下
angular.module('MyApp',['angularFileUpload'])
.factory('MyFactory', ['$http',function($http) {
return{
get: function(callback){
$http.get('my-url')
.success(function(data) {
callback(data);
});
}
};
}])
.controller('GlobalController',['$scope','$http','MyFactory',function($scope,$http,MyFactory){
SchoolFactory.get(function(data){
console.log(data);
});
// Few Function which will be called in all pages
}]);
现在,当使用包含上述JS的模板创建新页面时,出现错误。这是我创建新的js文件的方法。
angular.module('MyApp',[])
.controller('SomeController',['$scope','$http',function($scope,$http){
// Few Function which will be called in all pages
}]);
任何包含此模块的页面,我都会在控制台中看到SomeController不是函数的错误。
请帮我。提前致谢。
这是我开始使用Angular JS时遇到的相同问题。您的代码几乎没有错误。
您可以在单个页面中创建多个angular.module,但是只有一个可以具有依赖项。
angular.module('MyApp',['angularFileUpload'])
angular.module('MyApp')
从第二个模块中删除空数组[]。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句