2016-06-21 7 views
2

Ich bin neu eckig. Ich schreibe eine einfache eckige Anwendung. Ich habe ein Modul und einen Controller für dieses Modul und eine Fabrik erstellt. Ich habe die Abhängigkeiten zum Controller hinzugefügt, aber ich bekomme einen Injektorfehler. Ich verstehe einfach nicht, wo ich falsch liege.angularjs zeigt Injektorfehler, obwohl alle Abhängigkeiten erwähnt werden

Dies ist mein Winkel Code:

var app = angular.module("bookApp",[]); 
app.config(['$interpolateProvider',function($interpolateProvider) { 
    $interpolateProvider.startSymbol('{[{'); 
    $interpolateProvider.endSymbol('}]}'); 
}]);;app.controller("mainController",["$scope", "$http", "titles",function($scope, $http, titles){ 
    $scope.titles = [{}]; 
    titles.getTitles(); 
}]); 
app.factory("titles",["$scope","$http",function($scope, $http){ 
    var getTitles = function(){ 
     $http.get("http://localhost:8000/getTitles") 
     .then(function(response) { 
      $scope.titles = response.data; 
     }); 
    } 

    var addTitle = function(){ 
     var data = $.param({ 
      json: JSON.stringify({ 
       title: $scope.book.bookname 
      }) 
     }); 
     $http.post("http://localhost:8000/addTitle",data).success(function(response, status){ 
      $scope.titles = response.data; 
     }); 
    } 
    return{ 
     getTitles: getTitles 
    } 

}]); 

Mein html:

<body ng-app="bookApp"> 
    <div class="container"> 
     <div> 
      <table id="recordTable" ng-controller="mainController"> 
       <thead> 
        <tr> 
         <th>S.No</th> 
         <th>Name</th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr ng-repeat="title in titles"> 
         <td>{[{title.id}]}</td> 
         <td>{[{title.bookname}]}</td> 
        </tr> 
       </tbody> 
      </table> 
     </div> 

    </div> 
</body> 

bekomme ich folgende Fehlermeldung:

http://errors.angularjs.org/1.5.7/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20titles 

Ich verstehe nicht das Problem hier. Der Service "Titel" ist da, aber es gibt immer noch Injektorfehler.

Antwort

1

Grundsätzlich hatten Sie $scope in einer Fabrik injiziert, die nicht in einer Fabrik injiziert werden kann. Sie müssen in der $scope Abhängigkeit vom Fabrik-Abhängigkeits-Array einen Werkscode entfernen.

If you look at the definition of factory/service, they are sigletone object which are applicable throughout the application to provide common functionality between different components of angular application.

Sie sollen den Umfang von der Fabrik sorgfältig entfernen, wie unten, und die große Veränderung, die ich in der Fabrik hergestellt wird Versprechen Aufgabe $http.get Methode zurückgegeben.

Fabrik

app.factory("titles",["$http",function($http){ 
    var getTitles = function(){ 
     return $http.get("http://localhost:8000/getTitles") 
     .then(function(response) { 
      return response.data; 
     }); 
    } 

    var addTitle = function(book){ 
     var data = $.param({ 
      json: JSON.stringify({ 
       title: book.bookname 
      }) 
     }); 
     return $http.post("http://localhost:8000/addTitle",data).success(function(response, status){ 
      return response.data; 
     }); 
    } 
    return{ 
     getTitles: getTitles, 
     addTitle: addTitle 
    } 

}]); 

-Controller

app.controller("mainController",["$scope", "$http","titles", 
    function($scope, $http, titles){ 
    //$scope.titles = [{}]; 
    titles.getTitles().then(function(data){ 
     $scope.titles = data; 
    }); 
    } 
]); 
+0

Sie sind immer noch '$ scope' in die Fabrik – Erevald

+0

My bad, vielen Dank für Köpfe vorbei. Vielen Dank –