Angularjs 如何外部调用service

我写了一段angularjs代码,在controller里面注入service,大致类似这个样子:

window.app = angular.module('myApp', []);
app.controller('Ctrl',function($scope, Service){
    $scope.test = Service.test();
});
app.factory('Service',function(){
    var test= 1;
    var service={};
    service.testing= function(){
        return test;
    }
});

这段代码被放到一个html的body里面的一个<script></script>标签下,测试可以实现。但是如果我把

app.factory('Service',function(){
    var test= 1;
    var service={};
    service.testing= function(){
        return test;
    }
});

提出来,单独放到一个a.js文件里面就没办法实现了。我试着<script language=javascript src=“/js/a.js”></script>放到header里面;放到body里面放有controller的script标签上面或者下面都不行。是不是需要把这个文件引用到script标签里面呢?要怎么写呢?

阅读 4.3k
1 个回答

在单独的js文件这样写: angular.module('myApp').factory('Service', function() {//...})

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进