Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法从外部js文件访问angular服务_Javascript_Angularjs_Dependency Injection_Angularjs Service_Angularjs Controller - Fatal编程技术网

Javascript 无法从外部js文件访问angular服务

Javascript 无法从外部js文件访问angular服务,javascript,angularjs,dependency-injection,angularjs-service,angularjs-controller,Javascript,Angularjs,Dependency Injection,Angularjs Service,Angularjs Controller,这是我的html: <html> <head> <link rel="stylesheet" type="text/css" href="css/style.css"> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> </head> <body> <script

这是我的html:

<html>
<head>
  <link rel="stylesheet" type="text/css" href="css/style.css">
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

</head>
<body>
<script type="text/javascript" src="services.js"></script>
 <div ng-app="myApp" ng-controller="myCtrl2">
   hello {{x}} 
 </div>
 <script>
  var app=angular.module("myApp", []);
  app.controller("myCtrl2", ['$scope','$location', function($scope, $location) {
    myService.set("world");
    $scope.x=myService.get();
  }]);
</script>
</body>
</html> 

为什么我没有得到hello world,而chrome开发者工具却说:ReferenceError:myService没有定义

你需要纠正两件事

  • 不要在html页面上重新创建
    myApp
    模块,这将破坏模块的旧注册组件。而是使用由
    services.js
    创建的现有
    myApp
    模块

    var app=angular.module("myApp", []);
    
    应该是

    var app=angular.module("myApp");
    
  • 在控制器中使用服务之前,请先注入服务

    app.controller("myCtrl2", ['$scope','$location', 'myService', 
        function($scope, $location, myService) {
    
    app.controller("myCtrl2", ['$scope','$location', function($scope, $location,myService) { 
    

  • 你需要纠正两件事

  • 不要在html页面上重新创建
    myApp
    模块,这将破坏模块的旧注册组件。而是使用由
    services.js
    创建的现有
    myApp
    模块

    var app=angular.module("myApp", []);
    
    应该是

    var app=angular.module("myApp");
    
  • 在控制器中使用服务之前,请先注入服务

    app.controller("myCtrl2", ['$scope','$location', 'myService', 
        function($scope, $location, myService) {
    
    app.controller("myCtrl2", ['$scope','$location', function($scope, $location,myService) { 
    

  • 您需要在控制器中注入服务

    var app=angular.module("myApp", []);
     app.controller("myCtrl2", ['$scope','$location','myService', function($scope, $location,myService) {
      myService.set("world");
      $scope.x=myService.get();
    }]);
    

    您需要在控制器中注入服务

    var app=angular.module("myApp", []);
     app.controller("myCtrl2", ['$scope','$location','myService', function($scope, $location,myService) {
      myService.set("world");
      $scope.x=myService.get();
    }]);
    

    您需要将服务注入控制器

    app.controller("myCtrl2", ['$scope','$location', 'myService', 
        function($scope, $location, myService) {
    
    app.controller("myCtrl2", ['$scope','$location', function($scope, $location,myService) { 
    

    您需要将服务注入控制器

    app.controller("myCtrl2", ['$scope','$location', 'myService', 
        function($scope, $location, myService) {
    
    app.controller("myCtrl2", ['$scope','$location', function($scope, $location,myService) {