我在寻找代码中 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'
});
});
然后我有一个 Controller
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});
}]);
还有一个工厂
var services = angular.module('test.services', ['ngResource']);
var baseUrl = 'http://localhost\\:8080';
services.factory('ProductFactory', function ($resource) {
return $resource(baseUrl + '/test/smart/:query', {}, {
query: { method: 'GET', isArray: true }
})
});
此代码导致在我的 Controller 中 $scope.products = ProductFactory.query({query: $scope.query});
行加载上述类型错误。
此外,在调试我的代码时,我发现 $routeParams.q
为空或未定义。
我想要的是在我的 Controller 中,$scope.products
变量变成一个包含查询结果的数组。
我做错了什么?
最佳答案
请尝试使用正确的函数参数,
controllers.controller('ProductCtrl', ['$scope', '$routeParams', 'ProductFactory',
function ($scope /*ProductFactory*/, $routeParams, ProductFactory) {
$scope.query = $routeParams.q;
$scope.products = ProductFactory.query({query: $scope.query});
}]);
关于javascript - 在angularJS中使用工厂方法时未定义不是函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24124768/