角度1.2我在哪里宣布错误?工厂没有定义

时间:2014-06-08 03:59:32

标签: javascript angularjs

请原谅我,如果这是重复的,我已经尝试搜索其他线程,但我看不到与此注释类似。

错误:factoryGet未定义

 var myapp = angular.module('myapp', ['ngRoute','myServices']);
 /* adds in the myServices module and its factory as a dependency */    
 myapp.controller(
   'mycontroller', 
   [
     '$scope',
     function($scope) { 
       $scope.mydata = factoryGet.query(); 
     }
   ]
 );

 myapp.config(specifyRoute);
 function specifyRoute ($routeProvider) {
   $routeProvider
     .when('/', {template: '<div>no template</div>'})
     .when('/pagea', {template: '<div>Page A</div>'})
     .when('/pageb', {template: '<div>Page B</div>'})
     .when('/pagec', {template: '<div>Page C</div>'});
 }

 // define a new module called myServices
 angular.module('myServices', ['ngResource'])
   .factory('factoryGet', function  ($resource) {
     return $resource(
       '/airports',   // server path
       { method: 'getTask', q: '*' }, // Query parameters
       {'query': { method: 'GET' }}
     );
   });

html很标准

 <div class="container">
   <a href="#" class="btn btn-default">Home</a>
   <a href="#/pagea" class="btn btn-default">Page A</a>
   <a href="#/pageb" class="btn btn-default">Page B</a>
   <a href="#/pagec" class="btn btn-default">Page C</a>
 </div>
 <div class="container" ng-controller="mycontroller">
   <div ng-view></div>
 </div>

1 个答案:

答案 0 :(得分:1)

尝试:

myapp.controller(
   'mycontroller', 
   [
     '$scope',
     'factoryGet', //declare your dependency
     function($scope,factoryGet) { //declare your dependency
       $scope.mydata = factoryGet.query(); 
     }
   ]
 );