无法在 Angular 解析内部路由器配置中注入我的服务
Unable to inject my service in Angular resolve inside router configuration
(我的 plunkr 代码位于 http://plnkr.co/edit/6KU3GblQtMdRAx3v3USV?p=preview)
我正在尝试创建一个搜索栏(在导航中),它最终应该会到达后端 REST API。单击输入 'alpha' 时的输入搜索按钮将触发到 products/search/0?searchtext=alpha
的路由
点击按钮触发路由变化,应该按如下方式解决:
.when("/products/search/:page", {
templateUrl: "products.html",
controller: "ProductsSearchController",
resolve: {
// Define all the dependencies here
ProdSearchServ : "ProdSearchService",
// Now define the resolve function
resultData : function(ProdSearchServ) {
return ProdSearchServ.searchProducts();
}
}
})
但是,我收到以下错误
angular.js:9784 Error: [$injector:unpr] Unknown provider: ProdSearchServProvider <- ProdSearchServ
我相信我正在按照惯例做大部分事情,可能是我遗漏了什么?
我正在复制下面的 script.js 代码(也在上面的 plnkr link 中)。它具有所有路由配置和定义的控制器。
(function(){
// jargoViewer Create a new Angular Module
// This would go into the html tag for index.html
var app = angular.module("jargoViewer", ["ngRoute"]);
app.config(function($routeProvider){
$routeProvider
.when("/main", {
templateUrl: "main.html",
controller: "NavController"
})
.when("/products/search/:page", {
templateUrl: "products.html",
controller: "ProductsSearchController",
resolve: {
// Define all the dependencies here
ProdSearchServ : "ProdSearchService",
// Now define the resolve function
resultData : function(ProdSearchServ) {
return ProdSearchServ.searchProducts();
}
}
})
.otherwise({redirectTo:"/main"});
});
}());
// Nav Controller
(function() {
var app = angular.module("jargoViewer");
var NavController = function($scope, $location) {
// Function to invoke the Prod search based on input
$scope.search = function() {
console.log("searchText : " + $scope.searchtext);
$location.path("products/search/0").search({searchtext: $scope.searchtext});
};
};
app.controller("NavController", NavController);
}());
// Define the Prod Search Service here
(function() {
// Get reference to the app
var app = angular.module("jargoViewer");
// Create the factory
app.factory('ProdSearchService', function($routeParams, $http, $q) {
var searchProducts = function() {
pageNum = 0;
searchParam = '';
if (('page' in $routeParams) && (typeof $routeParams.page === 'number')) {
pageNum = $routeParams.page;
}
// Check if the router Param contains the field searchtext, if so, check if its a string
if (('searchtext' in $routeParams) && (typeof $routeParams.searchtext === 'string')) {
searchParam = $scope.routeParam.searchtext;
}
// Now make the http API hit to fetch the products
var request = $http({
method: "get",
url: "http://abcd.com/products/search/" + pageNum,
params: {
search: searchParam
},
});
return(request.then(handleSuccess, handleError));
};
function handleError(response) {
// The API response from the server should be returned in a
// nomralized format. However, if the request was not handled by the
// server (or what not handles properly - ex. server error), then we
// may have to normalize it on our end, as best we can.
if (
! angular.isObject(response.data) ||
! response.data.message
) {
return($q.reject( "An unknown error occurred."));
}
// Otherwise, use expected error message.
return($q.reject(response.data.message));
}
// I transform the successful response, unwrapping the application data
// from the API response payload.
function handleSuccess(response) {
if(response.data.error == true) {
return($q.reject(response.data.message));
}
return(response.data.data);
}
return {
searchProducts : searchProducts
};
});
}());
// Define the Products Search Controller below
(function() {
var app = angular.module("jargoViewer");
//var ProductController = function($scope) {
var ProductsSearchController = function($scope, $routeParams, ProdSearchService) {
// Nothing to do really here
};
app.controller("ProductsSearchController", ProductsSearchController);
}());
我觉得你说的时候不需要定义依赖关系
// Define all the dependencies here
ProdSearchServ : "ProdSearchService",
只需这样做:
.when("/products/search/:page", {
templateUrl: "products.html",
controller: "ProductsSearchController",
resolve: {
resultData : function(ProdSearchService) { //as you defined it before
return ProdSearchService.searchProducts();
}
}
})
有个类似的问题
这是由于您奇怪的命名约定造成的。有时 ProdSearchServ
有时 ProdSearchService
.
如果您只选择一个并始终如一地使用它,那么您就不会 运行 陷入这些类型的错误。
特别是您创建了名为 ProdSearchService
的服务,然后尝试以不同的名称使用它:
app.factory('ProdSearchService',
//vs
resultData : function(ProdSearchServ) {
我想你的印象是这段代码会为你修复它。但是,这仅适用于传递给控制器的依赖项,而不适用于一般的功能。对于已经存在的服务,不需要像这样专门定义;而不是简单地在控制器中使用正确的名称。
// Define all the dependencies here
ProdSearchServ : "ProdSearchService",
(我的 plunkr 代码位于 http://plnkr.co/edit/6KU3GblQtMdRAx3v3USV?p=preview)
我正在尝试创建一个搜索栏(在导航中),它最终应该会到达后端 REST API。单击输入 'alpha' 时的输入搜索按钮将触发到 products/search/0?searchtext=alpha
的路由点击按钮触发路由变化,应该按如下方式解决:
.when("/products/search/:page", {
templateUrl: "products.html",
controller: "ProductsSearchController",
resolve: {
// Define all the dependencies here
ProdSearchServ : "ProdSearchService",
// Now define the resolve function
resultData : function(ProdSearchServ) {
return ProdSearchServ.searchProducts();
}
}
})
但是,我收到以下错误
angular.js:9784 Error: [$injector:unpr] Unknown provider: ProdSearchServProvider <- ProdSearchServ
我相信我正在按照惯例做大部分事情,可能是我遗漏了什么?
我正在复制下面的 script.js 代码(也在上面的 plnkr link 中)。它具有所有路由配置和定义的控制器。
(function(){
// jargoViewer Create a new Angular Module
// This would go into the html tag for index.html
var app = angular.module("jargoViewer", ["ngRoute"]);
app.config(function($routeProvider){
$routeProvider
.when("/main", {
templateUrl: "main.html",
controller: "NavController"
})
.when("/products/search/:page", {
templateUrl: "products.html",
controller: "ProductsSearchController",
resolve: {
// Define all the dependencies here
ProdSearchServ : "ProdSearchService",
// Now define the resolve function
resultData : function(ProdSearchServ) {
return ProdSearchServ.searchProducts();
}
}
})
.otherwise({redirectTo:"/main"});
});
}());
// Nav Controller
(function() {
var app = angular.module("jargoViewer");
var NavController = function($scope, $location) {
// Function to invoke the Prod search based on input
$scope.search = function() {
console.log("searchText : " + $scope.searchtext);
$location.path("products/search/0").search({searchtext: $scope.searchtext});
};
};
app.controller("NavController", NavController);
}());
// Define the Prod Search Service here
(function() {
// Get reference to the app
var app = angular.module("jargoViewer");
// Create the factory
app.factory('ProdSearchService', function($routeParams, $http, $q) {
var searchProducts = function() {
pageNum = 0;
searchParam = '';
if (('page' in $routeParams) && (typeof $routeParams.page === 'number')) {
pageNum = $routeParams.page;
}
// Check if the router Param contains the field searchtext, if so, check if its a string
if (('searchtext' in $routeParams) && (typeof $routeParams.searchtext === 'string')) {
searchParam = $scope.routeParam.searchtext;
}
// Now make the http API hit to fetch the products
var request = $http({
method: "get",
url: "http://abcd.com/products/search/" + pageNum,
params: {
search: searchParam
},
});
return(request.then(handleSuccess, handleError));
};
function handleError(response) {
// The API response from the server should be returned in a
// nomralized format. However, if the request was not handled by the
// server (or what not handles properly - ex. server error), then we
// may have to normalize it on our end, as best we can.
if (
! angular.isObject(response.data) ||
! response.data.message
) {
return($q.reject( "An unknown error occurred."));
}
// Otherwise, use expected error message.
return($q.reject(response.data.message));
}
// I transform the successful response, unwrapping the application data
// from the API response payload.
function handleSuccess(response) {
if(response.data.error == true) {
return($q.reject(response.data.message));
}
return(response.data.data);
}
return {
searchProducts : searchProducts
};
});
}());
// Define the Products Search Controller below
(function() {
var app = angular.module("jargoViewer");
//var ProductController = function($scope) {
var ProductsSearchController = function($scope, $routeParams, ProdSearchService) {
// Nothing to do really here
};
app.controller("ProductsSearchController", ProductsSearchController);
}());
我觉得你说的时候不需要定义依赖关系
// Define all the dependencies here
ProdSearchServ : "ProdSearchService",
只需这样做:
.when("/products/search/:page", {
templateUrl: "products.html",
controller: "ProductsSearchController",
resolve: {
resultData : function(ProdSearchService) { //as you defined it before
return ProdSearchService.searchProducts();
}
}
})
有个类似的问题
这是由于您奇怪的命名约定造成的。有时 ProdSearchServ
有时 ProdSearchService
.
如果您只选择一个并始终如一地使用它,那么您就不会 运行 陷入这些类型的错误。
特别是您创建了名为 ProdSearchService
的服务,然后尝试以不同的名称使用它:
app.factory('ProdSearchService',
//vs
resultData : function(ProdSearchServ) {
我想你的印象是这段代码会为你修复它。但是,这仅适用于传递给控制器的依赖项,而不适用于一般的功能。对于已经存在的服务,不需要像这样专门定义;而不是简单地在控制器中使用正确的名称。
// Define all the dependencies here
ProdSearchServ : "ProdSearchService",