小编Pie*_*rDB的帖子

在angularJS中使用工厂方法时,未定义不是函数错误

TypeError: undefined is not a function在我的代码中找到解决方案时遇到了问题.我有以下app.js:

var app =  angular.module('test', ['ngRoute', 'test.services', 'test.directives', 'test.controllers']);
app.config(function ($routeProvider, $httpProvider, $locationProvider) {

  $locationProvider.html5Mode(true);

  $routeProvider.
    when('/q/:q', {
      templateUrl: '/templates/productlist.html',
      controller: 'ProductCtrl'
    }).
    otherwise({
      redirectTo: '/q'
    });

});
Run Code Online (Sandbox Code Playgroud)

然后我有一个控制器

var controllers = angular.module('test.controllers', []);
controllers.controller('ProductCtrl', ['$scope', '$routeParams', 'ProductFactory',
  function ($scope, ProductFactory, $routeParams) {

    $scope.query = $routeParams.q;
    $scope.products = ProductFactory.query({query: $scope.query});
  }]);
Run Code Online (Sandbox Code Playgroud)

还有一家工厂

var services = angular.module('test.services', ['ngResource']);
var baseUrl = 'http://localhost\\:8080';
services.factory('ProductFactory', function ($resource) {
    return $resource(baseUrl + '/test/smart/:query', {}, {
        query: { …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angularjs-service angularjs-routing

5
推荐指数
1
解决办法
6399
查看次数