Javascript 如何在AngularJS中创建服务?

Javascript 如何在AngularJS中创建服务?,javascript,json,angularjs,onsen-ui,Javascript,Json,Angularjs,Onsen Ui,这是我在angular.js中使用服务的代码 我得到了这个未捕获的错误:[ng:areq] </ons-toolbar> <div ng-controller="testAppController"> Search: <input ng-model="query" type="text" class="text-input" id="my-input"/> <table> <tr>

这是我在angular.js中使用服务的代码 我得到了这个未捕获的错误:[ng:areq]

</ons-toolbar>
        <div ng-controller="testAppController">
          Search: <input ng-model="query" type="text" class="text-input" id="my-input"/>
    <table>
      <tr>
        <th>Country</th>
        <th>Population</th>
      </tr>
      <tr ng-repeat="country in countries | filter:query">
        <td>{{country.name}}</td>
        <td>{{country.population}}</td>
      </tr>
    </table>
 </div>

 <div ng-include='"partials/footer.html"'></div>
    </ons-page>
这里

你需要换成

.controller('testAppController',['$scope','helloworldservice',function($scope,helloworldservice)
请在此阅读更多内容

angular.module('testsapp',[]).service('helloworldservice',function($http){
this.getDatafunction=函数(){
$http.get('json/countries.json')。
成功(功能(数据){
警惕(“成功”);
}).
错误(函数(数据){
警惕(“错误”);
});
}
}).controller('testAppController',['$scope','helloworldservice',和,
函数($scope,helloworldservice){
helloworldservice.getDatafunction();
}
]);

搜索:
国家
人口
{{country.name}
{{国家人口}

控制器定义中缺少$scope服务名称:

.controller('testAppController',['$scope', 'helloworldservice',function($scope,helloworldservice){
   helloworldservice.getDatafunction();
}]);
.service('helloworldservice',['$http', function($http){
  this.getDatafunction = function(){
    $http.get('json/countries.json')
      .success(function(data) {
         alert("sucesss");
      })
      .error(function(data) {
        alert("wrong");
      });     
  }
}])
请记住,如果将服务名称指定为参数的字符串,则必须为函数指定所有参数。建议指定要注入的服务名称

考虑到这一点,最好对服务定义中的$http参数执行相同的操作:

.controller('testAppController',['$scope', 'helloworldservice',function($scope,helloworldservice){
   helloworldservice.getDatafunction();
}]);
.service('helloworldservice',['$http', function($http){
  this.getDatafunction = function(){
    $http.get('json/countries.json')
      .success(function(data) {
         alert("sucesss");
      })
      .error(function(data) {
        alert("wrong");
      });     
  }
}])

你能把整个错误放在哪里吗?未捕获错误:[ng:areq]你把ng应用放在哪里了?ng应用指令在你的html中的任何地方了吗?我这样加的