我读了一篇关于"动态工厂"的回答。来自How to create Dynamic factory in Angular js?
它的效果很好,但我项目所需的一些额外条件让我感到疲惫。
条件很简单。
以上简单示例是http://jsfiddle.net/chaht01/QM52v/29/
下面的jscode
var app = angular.module("myApp", [])
.service("echo", function() {
return {
echo: function(msg) {
console.log(msg);
return msg;
}
};
}), makeService = function(module, identifier) {
module.factory(identifier+'-service', ['echo', function(echo) {
return {
run: function(msg) {
return echo.echo(identifier + ": " + msg);
}
};
}]);
};
makeService(app, 'ex1');
app.controller("myController", ['ex1-service',
'$injector',
'$scope',
function(service, $injector, $scope) {
$scope.service1 = service.run('injected.');
$scope.test = function(){
$scope.isReady = true;
makeService(app, 'ex2');
}
}]).controller("child",['ex2-service','$scope',function(service,$scope){
$scope.service2 = service.run('dynamically injected')
}])
当我点击按钮时,得到错误,如
Error: [$injector:unpr] Unknown provide: ex2-serviceProvider <- ex2-service
我该如何解决这个问题?