在angularJS中使用工厂方法时,Undefined不是函数错误
Undefined is not a function error when using factory method in 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'
});
});
然后是控制器
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 }
})
});
这段代码导致在我的控制器$scope.products = ProductFactory.query({query: $scope.query});
行上的上述TypeError加载。此外,在调试我的代码时,我看到$routeParams.q
为空或未定义。我想要的是,在我的控制器中,$scope.products
变量成为查询结果的数组。
我做错了什么?
请尝试使用正确的函数参数,
controllers.controller('ProductCtrl', ['$scope', '$routeParams', 'ProductFactory',
function ($scope /*ProductFactory*/, $routeParams, ProductFactory) {
$scope.query = $routeParams.q;
$scope.products = ProductFactory.query({query: $scope.query});
}]);
相关文章:
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- /undefined在我的404错误日志中多次出现
- 为什么会出现错误,“UncaughtTypeError:undefined不是函数”
- 未捕获的类型错误:undefined不是函数$
- Bootstrap-“;未捕获的类型错误:undefined不是函数;
- 尝试使用.ninnerHTMLlog打印数据,获取错误Uncaught TypeError:undefined不是函数
- 得到“;undefined不是函数“;错误
- NodeJS:zlib.gunzip(body)错误地返回“undefined”
- 将服务中的函数绑定到$scope(错误:无法设置undefined的属性'onChange')
- 未捕获的类型错误:undefined不是函数
- HTML5拖拽&drop正在抛出undefined不是函数错误
- 我得到了这个错误,但是不要;我不知道如何修复它,www.example.com/undefined?1460018627
- d3.js voronoi.clipExtent()返回错误:undefined不是函数
- 将 jQuery 脚本加载更改为异步后出现“$ is undefined”错误
- 从另一个调用原型方法-得到'undefined'错误
- 使用RegisterClientScriptBlock在页面末尾注册一个脚本,以避免sys undefined错误
- CSS with JavaScript -无法读取属性'undefined'错误
- 什么会导致'消息:'console'undefined"错误突然出现
- 插入对象时myStorage是undefined错误
- jQuery alert 'undefined'错误而不是Java/Spring异常详细信息